Where unmanaged resources are allocated

c#, garbage-collection

Solution

Essentially the heap is the same speaking from the operating system view: the memory space assigned to the OS process.

The difference is that when the CLR (.net VM) loads inside a Windows process it takes a piece of this heap and turns it into a managed heap. This memory space becomes the place where all managed resources are allocated and known to the garbage collector.

For instance, you can run into a Out of Memory error if you allocate a big chunk of unmanaged memory and run out of space for your managed heap. Or the other way around.

Jeffrey Richter is the guy that better explains this stuff. I highly recommend reading his explanation:

- Garbage Collection: Automatic Memory Management in the Microsoft .NET Framework

- Garbage Collection Part 2: Automatic Memory Management in the Microsoft .NET Framework

You can use the services of the System.InteropServices namespace, the Marshal class specifically, to copy data between the unmanaged part of the heap and the managed.

Problem

I am not a comp science guy. Managed resources are allocated on the heap. But I would like to know where unmanaged resources are allocated. If unmanaged resources are also allocated on the heap, is it the same heap used by managed resources or a different one? Thanks in advance. Harsha

Original source