Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Heap Memory Management .Net

When creating 2 objects of the same type, will the handle from the stack memory point to the same object in the heap or will it point to 2 separate objects. For clarity here is the specific question...

class Q2 {
   private static int num = 0;
   private String prefix;

   public Q2 (String p)
    { prefix = p; }

   public String Msg (String str) {
      String n;
      num++;   
      n = num.ToString();
     return n + " - " + prefix + str;
   }
}

Using an appropriate diagram, describe the state of memory after all of the following statements have been executed.

 Q2 var1, var2;
   var1 = new Q2("Question 2");
   var2 = new Q2 ("Another view");

Here are the answers I cannot decide between:

1 object:

enter image description here

2 objects:

enter image description here

like image 694
aelsheikh Avatar asked Dec 12 '11 19:12

aelsheikh


People also ask

What is heap memory management?

The heap is an area of dynamically-allocated memory that is managed automatically by the operating system or the memory manager library. Memory on the heap is allocated, deallocated, and resized regularly during program execution, and this can lead to a problem called fragmentation.

How does .NET manage memory?

Memory allocation NET framework that allocates and releases memory for your . NET applications. When a new process is started, the runtime reserves a region of address space for the process called the managed heap. Objects are allocated in the heap contiguously one after another.

Does C# have memory management?

NET Framework that allocates and releases memory for your . NET applications. The Common Language Runtime (CLR) manages allocation and deallocation of a managed object in memory. C# programmers never do this directly, there is no delete keyword in the C# language.

What is heap in asp net?

Heaps, Heaps and more HeapsNet stores most of the variables you will create (except for the value types) on a data-structure called the heap. This lives in the process address space and grows as more and more variables are needed and allocated by the application. The key is the 'growing when needed'.


1 Answers

To help clarify the discussion on the heaps here, there are about 8 different heaps that the CLR uses:

  1. Loader Heap: contains CLR structures and the type system
  2. High Frequency Heap: statics, MethodTables, FieldDescs, interface map
  3. Low Frequency Heap: EEClass, ClassLoader and lookup tables
  4. Stub Heap: stubs for CAS, COM wrappers, P/Invoke
  5. Large Object Heap: memory allocations that require more than 85k bytes
  6. GC Heap: user allocated heap memory private to the app
  7. JIT Code Heap: memory allocated by mscoreee (Execution Engine) and the JIT compiler for managed code
  8. Process/Base Heap: interop/unmanaged allocations, native memory, etc

HTH

like image 157
Dave Black Avatar answered Sep 28 '22 18:09

Dave Black