There are different aspects to understand unmanaged memory
.
In the higher level, the applications you run can be classified into two categories: unmanaged code
and managed code
. The managed code
is managed by a runtime
, like .Net and Java. The runtime provides important functionalities like automatic memory management(by garbage collector). And the unmanaged code
is the way you run a c/c++ program. The developers have the full responsibility to manage everything of the program, including the memory part(by APIs like malloc
and free
).
In a lower and specific level, for example, in .Net world, the memory management can also be classified into managed
and unmanaged
two parts. This is in fact the confusing point. Because as mentioned above, the runtime of .Net provide GC which can handle the memory allocation and release automatically. Why there are still unmanaged
memory? The critical point here is that the unmanaged
part is not the memory itself, instead it's the underlying resources bound to the memory. Generally, these are operating system level resource, like file
, network connection
and database connection
. The GC can collect the memory but it doesn't know how to handle those underlying system level resource. So to some extend, it's out of control of the runtime. Then the developer need to do something to help the runtime to handle this case properly(For example, the Dispose
method call in .Net world).