C #: managing large memory buffers

I support a video application written in C #. I need as much control as possible over memory allocation / deallocation for large memory buffers (hundreds of megabytes).

As written, when the pixel data needs to be freed, the pixel buffer is null. Is there a better way to free memory? Is there a high cost of garbage collecting large objects?

Thanks!

+4
source share
3 answers

The garbage collection cost of large objects is very high from what I remember. From what I'm reading, they automatically become generation 2 when distributed (they are allocated in a large heap of an object). And since they are large, they force a frequent collection of 2 generations.

Therefore, I would prefer to implement a manual pool for raster image arrays or even use unmanaged memory. Have some pool class and return the array back to it in Dispose your pix / bitmap class.

+1
source

Do not throw big buffers like this, you are lucky to have it. Video provides many opportunities for reuse. Do not lose the buffer until you are sure that you no longer need it. At this moment, it does not matter when it is collected.

+3
source

With memory blocks that are large (“hundreds of megabytes”), it should be relationally easy to find out who uses them and where (you can put 10–20 such blocks into memory anyway). Since ypu plans to use such amounts of mmeory, you need to carefully reduce memory usage - i.e. A simple copy of the entire buffer will take non-trivial time.

When you are done with a specific block, you can force the GC. This sounds like a reasonable use of the GC.Collect API - you did using a huge portion of all avaialble memory.

You can also consider switching to smaller (64k) blocks and linking them together if it works for your application. This is more consistent with garbage collection and can provide more flexibility for your application.

+1
source

Source: https://habr.com/ru/post/1342680/


All Articles