Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where are .NET local variables stored?

In IL, you can define local variables using the .locals directive. Where are these variables stored, stack or heap?

like image 367
thecoop Avatar asked Sep 29 '10 14:09

thecoop


2 Answers

It is very much an implementation detail of the JIT compiler. It will try very hard to store local variables in a CPU register, very efficient. The stack is the usual backing store, in case there aren't enough registers available to store all the local variables.

Big difference between the x86 and x64 jitters for example. x64 has a lot more registers available. This also applies to the arguments passed to a method. x86 allows 2 passed in a CPU register, x64 allows 4. Plus whatever can be stored in the FPU stack or XMM registers. So, there are really four distinct places a local variable can be stored.

like image 183
Hans Passant Avatar answered Nov 08 '22 14:11

Hans Passant


On the stack with parameters. ..BUT .....
1) for reference types, only the reference is stored on the stack not the object its referring to. The actual object is stored on the heap.
2) for value types, the actual value is stored on the stack.

Now when the execution flow in the method reaches the closing brace the value type data on the stack is destroyed there and then , while the reference type objects on the heap (whose references were here on this method's stack ) are handed over to the garbage collection system for collection at an appropriate time decided by the garbage collector itself.

like image 9
explorer Avatar answered Nov 08 '22 14:11

explorer