.NET 局部变量存储在哪里?

发布于 2024-09-25 09:15:40 字数 65 浏览 1 评论 0原文

在 IL 中,您可以使用 .locals 指令定义局部变量。这些变量存储在哪里,栈还是堆?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

深陷 2024-10-02 09:15:40

它很大程度上是 JIT 编译器的实现细节。它会非常努力地将局部变量存储在CPU寄存器中,非常高效。堆栈是通常的后备存储,以防没有足够的寄存器可用于存储所有局部变量。

例如,x86 和 x64 抖动之间存在很大差异。 x64 有更多可用的寄存器。这也适用于传递给方法的参数。 x86 允许在 CPU 寄存器中传递 2 个,x64 允许 4 个。加上可以存储在 FPU 堆栈或 XMM 寄存器中的任何内容。因此,实际上有四个不同的位置可以存储局部变量。

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.

允世 2024-10-02 09:15:40

在带有参数的堆栈上。 ..但是 .....
1)对于引用类型,只有引用存储在堆栈中,而不存储其引用的对象。实际对象存储在堆上。
2)对于值类型,实际值存储在堆栈中。

现在,当方法中的执行流程到达右大括号时,堆栈上的值类型数据将被销毁,而堆上的引用类型对象(其引用在此方法的堆栈上)由垃圾收集器自己决定合适的时间,交给垃圾收集系统进行收集。

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.

梦境 2024-10-02 09:15:40

如果对象不是值类型,则将其分配在堆上,并将对其的引用存储在堆栈上。否则直接在栈上分配。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文