Unmanaged memory and Managed memory
Asked Answered
D

2

50

what exactly are un-managed and managed memory? can anybody explain me in brief?

Also, what exactly would mean when the managed-memory concept is taken to RAM, calling managed-RAM. What are some of the specifics about "managed RAM" and "un-managed-RAM"?

Dent answered 28/8, 2009 at 6:40 Comment(0)
D
76

It is all the same physical memory. The difference is who is controlling it.

The Microsoft definition is that managed memory is cleaned up by a Garbage Collector (GC), i.e. some process that periodically determines what part of the physical memory is in use and what is not.

Unmanaged memory is cleaned up by something else e.g. your program or the operating system.

The term unmanaged memory is a bit like the World War 1, it wasn't called that until after World War 2. Previously it was just memory.

Dirge answered 28/8, 2009 at 7:15 Comment(1)
Behind the scenes, the .NET runtime/ JVM uses unmanaged memory as well. It is allocated from the memory manager of the OS - just like any unmanaged program would do. Simply spoken, it allocates a larger piece of memory as needed and calls it 'managed heap'. That block is then 'managed' for usage by managed objects with the help of the runtime (GC). The runtime takes also care of in-/decreasing the size and returns the memory to the OS when done. So I would rather call 'managed' memory being a virtual part of 'unmanaged' memory.Fondle
M
1

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).

Macedonia answered 20/3, 2023 at 2:37 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.