V8 中的堆栈和堆(JavaScript)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6602864/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
stack and heap in V8 ( JavaScript)
提问by Vyacheslav Egorov
does V8 uses stack and heap like the JVM? if so does it put primitives on the stack and objects on the heap?
V8 是否像 JVM 一样使用堆栈和堆?如果是这样,它会将原语放在堆栈上并将对象放在堆上吗?
采纳答案by Mathias Schwarz
Yes, V8 uses a heap similar to JVM and most other languages. This, however, means that local variables (as a general rule) are put on the stack and objects in the heap. This may for instance not hold if a function closes over these values. As in the JVM, primitives can only be stored on the stack if they are stored in a local variable.
是的,V8 使用类似于 JVM 和大多数其他语言的堆。然而,这意味着局部变量(作为一般规则)被放在堆栈和堆中的对象上。例如,如果函数关闭这些值,则这可能不成立。与在 JVM 中一样,如果原语存储在局部变量中,则它们只能存储在堆栈中。
As a user it is not something you would normally need to worry about.
作为用户,这不是您通常需要担心的事情。
回答by Vyacheslav Egorov
- In V8
null
,undefined
,true
andfalse
internally are heap allocated objects. If you are comming from Java you can say thattrue
andfalse
in V8 are more likeBoolean.TRUE
andBoolean.FALSE
in Java. - There is an important difference between reallocal variables and variables that are captured by closures or shadowed by eval/with. Captures variables are stored in a special heap allocated structure called Contextand are accessed indirectly. For more details about realvs. context allocates variables see my answer to a different question
V8 has two compilers: non-optimizing (aka full) and optimizing one:
- Non-optimizing compiler can't store floating point numbers and integers beyond 31-bit (32-bit on x64) on the stack it always boxes them into HeapNumbers. It does not try to do register allocation and stores reallocal variables on the stack.
- Optimizing compiler is much smarter. It does register allocation (linear scan) and can keep full 32-bit integers and floating point numbers on the stack and in the registers (including XMM registers).
Speaking of JVM: it can perform so called stack allocationand allocate a non-escaping object on the stack instead of the heap. A more generic optimization (scalar replacement) can sometimes completely eliminate allocation of non-escaping object and explode it into separate fields.
- 在 V8
null
中undefined
,,true
和false
内部是堆分配的对象。如果您是从正在添加Java的,你可以说,true
和false
在V8更像是Boolean.TRUE
和Boolean.FALSE
Java编写的。 - 真正的局部变量与被闭包捕获或被 eval/with 遮蔽的变量之间有一个重要的区别。捕获变量存储在称为Context的特殊堆分配结构中并被间接访问。有关真实与上下文分配变量的更多详细信息,请参阅我对不同问题的回答
V8 有两个编译器:非优化(又名完整)和优化一个:
- 非优化编译器无法在堆栈上存储超过 31 位(x64 上为 32 位)的浮点数和整数,它总是将它们装箱到HeapNumber 中。它不会尝试进行寄存器分配,而是在堆栈上存储真正的局部变量。
- 优化编译器要聪明得多。它进行寄存器分配(线性扫描),并且可以在堆栈和寄存器(包括 XMM 寄存器)中保留完整的 32 位整数和浮点数。
说到 JVM:它可以执行所谓的堆栈分配,并在堆栈而不是堆上分配一个非转义对象。更通用的优化(标量替换)有时可以完全消除非转义对象的分配并将其分解为单独的字段。
回答by Krishna Ganeriwal
In the most general terms, Yes V8 uses a heap and stack for functioning wherein general local variables are stored in the stack while the objects that need to be maintained are stored in the heap.
用最一般的术语来说,Yes V8 使用堆和堆栈来运行,其中一般局部变量存储在堆栈中,而需要维护的对象存储在堆中。