Vinit Sankhe
Vinit Sankhe

Reputation: 19885

Managed and Unmanaged heap

What is an unmanaged heap?

I thought any object based memory that the CLR manages was the managed heap, so why do we talk about an unmanaged heap?

Upvotes: 6

Views: 8858

Answers (3)

GingerJack
GingerJack

Reputation: 3134

In order to understand unmanaged Heap in .net we need to get an idea of what Managed Heap is.

In .net framework we have Garbage collector which is initialized by Common language routine.During its initialization Garbage collector allocates a segment of memory to store and manage objects instantiated by a Managed code .This memory is called the managed heap, as opposed to a native heap in the operating system.

Unmanaged heap is the one being used by unmanaged code/native code for allocating memory at run time.This heap is not under the control of garbage collector and it needs to be handled by the developer for freeing the memory allocated. Difference between "managed" and "unmanaged"

Upvotes: 0

Schroedingers Cat
Schroedingers Cat

Reputation: 3129

As per John Skeet - the managed heap is the one that .net will manage for you, that all standard objects are created on, that you normally don't need to bother too much about because it is managed.

unmanaged means that you are personally allocating memory, and so you are personally responsible for deallocating it, managing it yourself, and keeping track of what is being used.

So yes, object memory ( in the sense of normal object creation and destruction, things that derive from object ) is managed. It is the other stuff you need to worry about - non objects and memory allocated for them.

Upvotes: 5

Jon Skeet
Jon Skeet

Reputation: 1500835

Imagine you call a Win32 function using P/Invoke, and that allocates some memory using malloc. The garbage collector has no visibility of that memory - it's unmanaged.

That may or may not be the context in which you've heard the term, of course - if you could point us to some examples, we may be able to help you more.

Upvotes: 11

Related Questions