views:

68

answers:

3

Hello all, 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

A: 

In memory. Like for any unmanaged process.. THe managed heap obviously is different from the managed one.

TomTom
+7  A: 

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:

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.

Sergio Acosta
It is a really good article. For completeness i also added the second part of it.
Oliver
@Oliver: excellent! thanks for editing the question instead of just posting it in a comment.
Sergio Acosta
@Sergio: Yes, that's one of the nice features when you earned enough rep (like you already did, too). ;)
Oliver
A: 

The CLR maintains its own heaps. Initially, two are created: one is simply called the managed heap (or small object heap) and the other is the large object heap (see also here). These managed heaps are physically separate from the native heaps allocated by the CRT for use with new and malloc.

You can use VMMap to inspect the different heaps allocated by a process.

Chris Schmich