1
Reply

How Memory is allocated for objects on the Managed Heap?

Santosh Kumar

Santosh Kumar

11y
1.2k
0
Reply

    When your applications starts up, the Garbage Collector (GC) allocates some virtual memory to use as a managed heap It creates a pointer to the next available free space in the managed heap, which defaults to point to the beginning of the (empty) heap.

    When you use the new operator to create a new reference-typed object, the Garbage Collector allocates space on the managed heap for your object as follows:

    1. It uses the pointer to the next available space on the heap to determine where to store your object
    2. It advances the next available free space pointer, based on the size of your object
    3. It zeroes out memory for the new object
    4. It passes back a pointer to the new object, which is then stored in the reference-typed variable that refers to your object
    5. The object's constructor executes, to initialize data members within the object