开发者

Where are .NET local variables stored?

开发者 https://www.devze.com 2023-01-17 21:51 出处:网络
In IL, you can define local variables using the开发者_开发知识库 .locals directive. Where are these variables stored, stack or heap?It is very much an implementation detail of the JIT compiler.It will

In IL, you can define local variables using the开发者_开发知识库 .locals directive. Where are these variables stored, stack or heap?


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.


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.


If the object is not a value type, it is allocated on the heap and a reference to it is stored on the stack. Otherwise, it is directly allocated on the stack.

0

精彩评论

暂无评论...
验证码 换一张
取 消