Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where unmanaged resources are allocated

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

like image 275
Harsha Avatar asked May 18 '10 07:05

Harsha


People also ask

What is unmanaged resources?

The most common types of unmanaged resources are objects that wrap operating system resources, such as files, windows, network connections, or database connections.

Can garbage collector claim unmanaged objects?

Now, it is important to note that the garbage collector cleans and reclaims unused managed objects only. It does not clean unmanaged objects.

How do you handle unmanaged resources in C#?

Unmanaged objects are wrapped around operating system resources like file streams, database connections, network related instances, handles to different classes, registries, pointers, etc. Unmanaged resources can be cleaned-up using 'Dispose' method and 'using' statement.

Which interface is used to clean up unmanaged resources?

IDisposable interface provides a mechanism for releasing unmanaged resources, so it should be used only if you use them like: file streams, database connections, sockets, memory allocation, etc. This is basically Explicit resources cleanup.


2 Answers

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.

like image 111
Sergio Acosta Avatar answered Nov 08 '22 12:11

Sergio Acosta


To operate with unmannaged heap use Marshal class described at MSDN

http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.marshal(v=vs.71).aspx

Unmanaged heap doesn't controlled by GC, so all responsibility of memory usage lies on you. Essentially to release memory that allocated for unmanaged resources use IDisposable interface and release all allocated resources by yourself. All unmanaged resources like SQL connections and different IO operations are using this approach.

like image 1
Igor Lozovsky Avatar answered Nov 08 '22 12:11

Igor Lozovsky