6

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?

trincot
  • 317,000
  • 35
  • 244
  • 286
WPF-it
  • 19,625
  • 8
  • 55
  • 71

3 Answers3

11

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.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
  • 1
    Ok so within CLR's context, there is no unmanaged heap. So it is just the memory used by the unmanaged code, that has to be exclusively disposed for a complete GC.... correct? – WPF-it Jul 08 '11 at 08:22
  • 2
    @user164184: I wouldn't say there *is* no unmanaged heap - more that we don't need to worry about it. The CLR itself will no doubt use unmanaged data structures. – Jon Skeet Jul 08 '11 at 08:29
  • Using C++/CLI it is even more easy. You can use `malloc` in your own code or simply write `new std::string();` to allocate something on the unmanaged heap. – Stephan Jul 08 '11 at 08:45
5

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.

Schroedingers Cat
  • 3,099
  • 1
  • 15
  • 33
0

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"

Community
  • 1
  • 1
GingerJack
  • 3,044
  • 1
  • 17
  • 19