Asia/Kolkata
BlogApril 26, 2024

Stack memory vs Heap memory

Arun Et
What's the difference? Why two different memories? What do they store? First let's start with the "Why". Stack and heap memory is a common concept of memory management not just in javascript, but across multiple programming languages.
  • A Fixed memory (stack) - to store local variables, function calls and its arguments.
  • A Dynamic memory (heap) - to store complex data structures, with longer lifetime and dynamically varying size.
Low level languages like C, C++ manually manages memory, developers have to specify when to allocate memory and when to de-allocate and free up memory. In High level languages like Java, Python, JS etc... memory management is maintained by their respective runtime environments. Like I mentioned this is maintained by their respective runtime, so browsers don't really have a say in this. It's the JS engine that makes and manages these two memories inside a browser. Now let's see the differences Stack memory if a fixed size memory to store local variables aka primitive type variables, reference to non-primitives that exist in the heap.
Stack Memory
Since stack memory is fixed or static, there is limit on how big a stack type can be. This stack size differs between different JS engines.
Browser
Engine
Typical Stack Limit
Chrome/EdgeV8~ 0.5 MB to 2 MB
Node.js (V8)V8~ 0.5 MB to 2 MB
FirefoxSpiderMonkey~ 1 MB
SafariJavaScriptCore~ 0.5 MB – 2 MB
Stack memory follows Last in First out method. Since the stack is of fixed size and folows the LIFO method, memory allocation is extremely fast compared to Heap. All the local variables gets cleared from memory once the function returns. Heap memory is dynamic, un-structured, long-lived and larger compared to stack. Since its unstructured, memory allocation is slower compared to stack. Heap is where objects, arrays, functions, and other non-primitive values are stored. Memory allocation for these data is determined during run-time. Even though Heap memory is dynamic, there is a maximum limit per tab set by the javascript engine to avoid system crash (running out of system memory), site becoming unresponsive and laggy (larger memory === longer GC operations).
Browser
Engine
Typical Heap Limit
Chrome/EdgeV8~ 4GB
Node.js (V8)V8~ 4GB to 8GB
FirefoxSpiderMonkey~ 4GB
SafariJavaScriptCore~ 4GB
System capabilities plays a role in this limit as well Stack - primitives; Heap - non-primitives; A function as a whole is stored in Heap memory, function's code and its properties . The variable that stores its reference exists in stack memory or in heap memory - in case of a function stored within an object.
Stack Vs Heap
When a function is called, a stack frame is created for that function, with all the local variables, function's arguments and any non-primitive references available in scope. This stack frame is then pushed onto the top of the stack. Once the function completes and returns, the stack frame gets popped off and along with it, all the local variable's memory and function argument's memory gets freed up. In the below example the user object is stored in heap, and it's values which are string and number types are also stored in the heap within the user object. But the variable "const user" that holds the reference to the user object exists in the stack.
Share this post: