extremely large objects and managed heap in C#

866 Views Asked by At

What will happen when i`ll try to save in physical memory very large objects, which are larger than managed heap? For example, the size of a film is 4.5 GB, and the size of virtual memory (RAM) is only 2 GB. How will garbage collector work in this case? (physical space is enough)

3

There are 3 best solutions below

0
On

Its clear that you will run out of memory and in this case you will get MemoryOutOfException and gc cannot do anything about that

0
On

What will happen when i`ll try to save in physical memory very large objects, which are larger than managed heap?

I don't know whether the CLR has evolved since I last checked, but as far as I'm aware even the 64-bit version of the v4 CLR doesn't support any single object being larger than 2GB, even if you've got plenty more physical memory.

0
On

In the .NET 4.5 CLR you can turn on support for objects >2GB in size through a config change. The array index is limited to int.MaxValue though so you can only create an object that large if you use a bigger element type than byte (which is not what you want in your case as you are storing bytes).

So on the current version (4.5) it is not even possible to allocate such an array. If it was, it would be stored on the LOH.

Anyway, if you need to store such a large file, and do not want to use a streaming approach, which generally would be preferable, I recommend you use Marshal.AllocHGlobal or VirtualAlloc. They are unsafe by nature, but they will not impose any limits.