The answer is CLR along with another important consistuent of the .NET framework called Garbage collector or simply the GC .
Before going to know how Garbage collector works, it is important to know how memory is allocated to variables in the .NET framework.
Variables in .NET can be broadly classified as two kinds. The value types that are derived from System.ValueType, which hold the data and reference types which hold a reference to the actual data.The CLR maintains the value types on stack and reference types are maintained in the managed heap.
Lets consider this simple C# example to examine how variables are managed in .NET,
Assume we have two variables as follows,
int iVariable = 100;
Object oRef = new Object();
As we have discussed earlier, iVariable is a value type and is stored on Stack.Value types hold the data in them. So, the stack structure of the stack after allocating space for iVariable could be like this.
Now, you might be guessing that variable oRef would be stored in the managed heap, but here comes the twist in the tale folks, oRef will also be stored on the stack, but instead of holding the data it will be holding the address of the managed heap location which has oRef's members.

