Automatic Memory Management

Objects are initially allocated on the heap when they are instantiated with the new keyword. Memory allocation is very efficient because objects are created sequentially on the heap. The heap has a special pointer that begins at position 0 on the heap and is incremented for the size of the allocated object. This establishes the beginning point for the next object to be allocated. The process continues until memory is full.

Figure 34.1 is a graphical representation of the heap after four objects—A, B, C, and D—have been allocated on the heap. After each allocation, the Next Object Pointer points to the top of the last allocated object, marking the location of the beginning of the next object to be allocated.

Figure ...

Get C# Unleashed now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.