Managing an empty buffer using C #

We need to develop some kind of buffer management for the application that we are developing using C #.

In fact, the application receives messages from devices as they arrive (maybe a lot in a short period of time). We need to queue them in some kind of buffer pool so that we can process them in a controlled manner.

We thought about allocating a block of memory in 256 byte blocks (all messages are smaller than this), and then, using buffer pool management, have a pool of available buffers that can be used for incoming messages and a buffer pool that is ready for processing.

Thus, the thread will "Get the buffer" (process it), "Free buffer" or "Leave in the pool." We also need to know when the buffer was filling.

Potentially, we also need a way to peer into the buffers to see that the buffer with the highest priority in the pool, and not always get the next buffer.

Is there support for this in .NET, or is there some kind of open source code that we could use?

+6
c # buffer pool
source share
5 answers

Managing memory on C # hard drives is actually pretty good, so instead of a buffer pool, you can just allocate exactly what you need and insert it into the queue. Once you're done with the buffer, just release the garbage collector.

Another option (very little about your application) is to process messages minimally as they are received and turn them into full-fledged objects (with priorities and all), then your turn could prioritize them just by examining the correct set of attributes or methods.

If your messages arrive too fast even for minimal processing, you may have two queues. One of them is just a queue of raw buffers, and the next queue is a queue of message objects built from buffers.

Hope this helps.

+4
source share

Why don't you just get the messages, create a DeviceMessage object (due to the lack of a better name) and put this object in the queue? If prioritization is important, use the PriorityQueue class, which handles this automatically (by arranging DeviceMessage objects in priority order when they are queued). This seems like a more OO approach and will simplify maintenance over time with respect to prioritization.

+2
source share

@grieve: Networking is native, which means that when buffers are used to receive / send data on the network, they are fixed in memory. see my comments below for more details.

+2
source share

I know this is an old post, but I think you should take a look at the memory pool implemented in the ILNumerics project. I think they did exactly what you need, and this is very good code. Download the http://ilnumerics.net/ code and see the ILMemoryPool.cs file

+2
source share

I'm doing something like that. I have messages in MTA flows that need to be served in STA flows.

I used a BlockingCollection (part of fx parallel extensions), which is controlled by several STA threads (configurable, but by default - xr * number of cores). Each thread tries to pull the message out of the queue. They are either a timeout or an attempt to retry or successfully display a message and serve it.

I connected it using perfmon counters to track downtime, uptime, incoming messages, etc., which you can use to configure queue settings.

You will need to implement a custom collection, or possibly extend BC to implement the priorities of the queue elements.

One of the reasons why I implemented it is because, as I understand it, queuing theory as a whole contributes to single-line, multiple -servers (why do I feel like I'm going to catch shit about this?).

+1
source share

All Articles