does V8 uses stack and heap like the JVM? if so does it put primitives on the stack and objects on the heap?
-
1Not making this a full answer since I didn't fully read the article, but there were definitely discussions of stack/heap/register usage in http://wingolog.org/archives/2011/07/05/v8-a-tale-of-two-compilers – Domenic Jul 06 '11 at 21:43
-
see http://stackoverflow.com/questions/16949690/many-erros-when-i-try-compile-c-in-v8-javascript/19088160#19088160 – wcy Sep 30 '13 at 07:00
3 Answers
- 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 real local variables and variables that are captured by closures or shadowed by eval/with. Captures variables are stored in a special heap allocated structure called Context and are accessed indirectly. For more details about real vs. 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 real local 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 allocation and 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.

- 1
- 1

- 10,302
- 2
- 43
- 45
-
@Randomblue sorry for the inconvenience! I updated links to use fixed revision numbers to avoid such a problem in the future. – Vyacheslav Egorov Sep 01 '12 at 17:09
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.
As a user it is not something you would normally need to worry about.

- 7,099
- 23
- 28
-
A lot of the details of the behavior at run time can vary based on the optimizations applied. Cf. http://code.google.com/apis/v8/design.html – Jared Updike Jul 06 '11 at 21:29
-
Actually as far as I know the stack vs. heap issue is pretty static in V8, but I am not 100% sure about it. The stuff you link to is about property access in objects which has little to do with this. – Mathias Schwarz Jul 06 '11 at 21:35
-
2when a function closes over locals the closure object is on the heap? Does the frame never move to the stack when its code is executed? – Jared Updike Jul 06 '11 at 22:11
-
1yeah I don't understand how closures work with the stack. closures are basically like objects in memory, I don't see how the local variables could be on the stack in the closure... – Alexander Mills Jul 29 '15 at 06:58
-
An object can be a local variable (your answer implies that objects can't be local), what happens then ? I assume they are still stored in the heap. – doubleOrt Jul 09 '18 at 07:42
-
Closed over variables are saved in context objects, of which there is a stack that builds in parallel to the function stack. And yes, even local objects are heap-allocated. Anything other than 31/32 bit integers (depending on 32/64 system) is a pointer to a heap-allocated object, due to V8's internal usage of tagged pointers. – Oct 25 '19 at 22:13
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.

- 1,903
- 19
- 17