Memory Management

The CLR provides automatic memory management. Essentially, this means that when developers create a value (i.e., an instance of any type), the CLR is responsible for allocating and freeing the memory in which that value is stored. Value types and reference types have their memory allocated and freed via different mechanisms, however.

Value Types Versus Reference Types

Value types are normally allocated on the program's stack. Whenever a parameter or a local variable is needed, such as when a method is called, memory for the value is allocated on the stack. This memory remains available until the method returns; the stack is then unwound and the memory is automatically reclaimed. This model of allocating and freeing stack-based ...

Get Programming in the .NET Environment 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.