This is a common problem of allocating a large number of small objects: the overhead of objects that you can usually ignore comes into play.
37 * 10 ^ 6 * 4 bytes = 148 MB
Assuming an array of four bytes occupies four bytes in memory, it is incorrect. In addition to a four-byte payload, an array object must store the length of the array, synchronization block, and type pointer. This is 12 bytes of overhead on a 32-bit system or 24 bytes on a 64-bit system.
In addition to the individual overhead of array objects, you need to consider the overhead of the memory allocator, the overhead of aligning the memory, and the overhead of the garbage collector. All things taken together, it is unreasonable to see that the total used memory is increased to 2 GB.
One way to fix this is to go to the list of uint s, which occupy four bytes each. When you need to save four bytes, convert them to uint and save them in a list. When you need your bytes, convert uint to a temporary four-byte array. Use BitConverter to deal with conversion between uint and byte s arrays.
dasblinkenlight
source share