Where are .NET local variables stored?

試著忘記壹切 提交于 2019-11-27 23:39:12

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!