What is the difference between managed heap and native heap in C # application

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

  • Managed: for managed applications, the profiler only collects information about the default managed heap. Managed heap profiling is done by collecting a set of ETW CLR events in the profiler.
  • Native: for its own applications, the profiler collects only information about the heap. To collect information about the native heap, we include a stack trace and a collection of heap traces (ETW), which are very verbose and will create large diagsession files.

My question is: In my C # program (I only have C # code with xaml files), which objects will be moved to the managed heap and which types will go into their own heap? And how can I specify the maximum size of each heap when starting my application? I guess GC only works on a managed heap, right?

+8
c #
source share
1 answer

When you create an object using a new statement in C # (or the corresponding statement in any other CLR language), the .NET runtime allocates memory in a โ€œmanaged heapโ€ (just a .NET-managed runtime heap + garbage collector). This is, in fact, one of two heaps - one is for objects smaller than 85 KB, and the other is for objects larger than this (large arrays, etc.). In any case, when such an object is selected, you do not return a real pointer describing the address of the allocated space, as in native code. What you get is a "handle", which is an indirect reference to this memory address. This direction exists because the actual memory location may change when the GC collects and compacts the heap.

If you want to talk to unmanaged / native code that a pointer expects, you need to use pointers rather than descriptors .. NET provides two methods for converting a .NET descriptor to a raw pointer that can be passed into unmanaged code.

Hope this helps!

+5
source share

All Articles