32

does V8 uses stack and heap like the JVM? if so does it put primitives on the stack and objects on the heap?

  • 1
    Not 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 Answers3

40
  • In V8 null, undefined, true and false internally are heap allocated objects. If you are comming from Java you can say that true and false in V8 are more like Boolean.TRUE and Boolean.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.

Community
  • 1
  • 1
Vyacheslav Egorov
  • 10,302
  • 2
  • 43
  • 45
15

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.

Mathias Schwarz
  • 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
  • 2
    when 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
  • 1
    yeah 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
-1

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.

Krishna Ganeriwal
  • 1,903
  • 19
  • 17