Suppose I have a byte[]
(buffer) and I need to keep it in memory. It also needs growing strategy x2
. So each time I need it to grow I have to create another byte[]
twice large and copy elements there.
During this operation the app memory usage is original_size * 3
because I have in memory both the original buffer and the new buffer.
When working with buffers of size near 300 mb this operation takes 900 mb of memory and can easily lead to OutOfMemoryException
in 32-bit app.
The only way to make this better I know is to choose another growing strategy: after some constant size make the buffer grow linearly (e.g. +5mb each time). But it still requires 600 mb for my 300 mb of data!
So can I do anything about it?
I think about structure which maintains a list of buffers inside so it doesn't need to copy anything when growing - just adds new buffer to the list. It should provide methods to perform operations with those buffers as with one big buffer. Is there something in .NET like this? Or usually what's it called like?
Added:
- I don't know the capacity preliminary.
- The buffers are used in serialization process.
- Sorry for misleading, the question is about buffer growing problem in generic - not about solving a particular app issue.