From this http://blogs.msdn.com/b/visualstudioalm/archive/2014/04/02/diagnosing-memory-issues-with-the-new-memory-usage-tool-in-visual-studio.aspx
My question is In my c# program (I only have c# code with xaml files) what kinds of object will goes to managed heap and what kinds goes to native heap? And how can I specify the max size of each heap when my application runs? I assume GC only runs on managed heap, is that correctly?
The managed heap After the garbage collector is initialized by the CLR, it allocates a segment of memory to store and manage objects. This memory is called the managed heap, as opposed to a native heap in the operating system. There is a managed heap for each managed process.
A managed memory environment, like the ART or Dalvik virtual machine, keeps track of each memory allocation. Once it determines that a piece of memory is no longer being used by the program, it frees it back to the heap, without any intervention from the programmer.
This is used for stackwalking. Ref Counting, so the GC knows when he can move an object to the next generation and so on...
Stack is a linear data structure whereas Heap is a hierarchical data structure. Stack memory will never become fragmented whereas Heap memory can become fragmented as blocks of memory are first allocated and then freed. Stack accesses local variables only while Heap allows you to access variables globally.
When you create an object using the new operator in C# (or the corresponding operator in any other CLR language), the .NET runtime allocates memory in the "managed heap" (simply a heap managed by the .NET runtime + the garbage collector). This is, in reality, one of two heaps - one meant for objects less then 85K in size and the other for objects larger than this (large arrays and the like). Either way, when such an object is allocated, you don't get back a real pointer describing the address of the allocated space like you would in native code. What you do get back is a "handle", which represents an indirection to that memory address. This indirection exists because the actual memory location may change when the GC collects and compacts the heap.
When you want to talk to unmanaged/native code that expects a pointer, however, you need to use pointers, not handles. .NET provides two methods to convert a .NET handle to a raw pointer that can be passed in to unmanaged code.
I hope this helps!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With