Memory management in Javascript

Low-level programming languages require you to allocate and free up memory manually while high-level programming languages handle it for us.
So, that brings the question, what is a "low-level" and "high-level" programming language?
Low-level languages, such as Assembly or C, operate very close to the machine code, giving programmers fine-grained control over system resources and hardware operations. This proximity to hardware makes them highly efficient but more complex and error-prone.
In contrast, high-level languages like Python, JavaScript, and Ruby abstract away much of the hardware detail, focusing on ease of use, readability, and productivity. They manage memory automatically and provide built-in functions and libraries that simplify complex tasks, making them more accessible to developers but sometimes at the cost of performance efficiency.
To help you imagine what I mean by that, imagine a semi-automatic washing machine vs a fully automatic washing machine. In the semi-automatic top load machine, you have to set the time, and mode manually, and then manually start the spin mode, etc. correct? But, in contrast, in a fully automatic machine, the machine handles it for you, you just have to let the machine know whether you want a quick wash or a strong wash, and the time, spin, water control, and everything else is "automagically" handled for you.
Languages like Javascript automatically allocate memory and free it up when it is not in use anymore, this process is called as "Garbage collection". Read about garbage collection in detail in our article on Garbage collection.
What is memory?
On a hardware level, a computer's memory has flip-flops. Each flipflop has transistors capable of storing a bit. Each of these has a unique identifier so that we can target them to read and write data. So, to help you imagine, it's like a huge array of bits, which we can access. Imagine this like small boxes, that are placed next to each other. Together they make a huge amount of space capable of storing items.
Since it's hard to access each bit and rather harder to remember and organize, we combine them ( not physically, but conceptually ). 8 bits make one byte.
Kinds of memory
There are mainly two kinds of memory ( in the context of this discussion ), Stack and Heap. Both are two different parts inside of the RAM ( Random Access Memory ). Yeah, the one that we have been struggling to get more of, for all of our lives!
The stack and heap are two distinct areas of memory used for different purposes.
Stack Memory: In JavaScript, the stack is used for managing function calls and local variables. When a function is invoked, a stack frame is created, holding information such as the function's parameters, local variables, and the point to return to after the function completes. This stack frame is pushed onto the stack, and when the function exits, the frame is popped off, and the memory is automatically freed.

Heap: The heap, on the other hand, is used for dynamic memory allocation in JavaScript. This region of memory handles the creation and management of objects and arrays whose sizes may not be known at compile time. That is how we can go without assigning the size at the beginning of the array creation and keep pushing the data to it afterward.
When you create a new object or array, JavaScript allocates memory on the heap to store it, and this memory must be managed carefully. JavaScript’s garbage collector automatically handles deallocating memory from the heap when it is no longer in use.
Where is the data stored?
Remember the learnings from the previous lecture on data types, we have two kinds of data types in Javascript, Primitive and Reference types.
The primitive types like String, Number, Boolean, and Null, are stored in the Stack, and Arrays, Functions, and Objects are stored in the Heap.
Phases of memory consumption
There are 3 phases of memory consumption:
- Allocation phase
- Use the memory ( read, write )
- Release the memory

- Allocation phase:
The memory can be allocated in a couple of ways such as Value initialization, Functions, etc.
Javascript does not require the programmer to allocate memory when a new variable(s) are created. Rather Javascript handles this for you. It is like how new boxes are brought just as new Laddus are prepared at home, well, kind of.
- Using the memory
Using the memory basically means, accessing the data from the memory, from these small boxes. Essentially, when we add data, access data or change the data say inside a function or a variable, it under-the-hood changes the data in one of these boxes.
Imagine, there are different Chocolate, and each box has one chocolate, now as we take a chocolate from a box, put a Chocolate into a box or just remove the mango bite and add a milky bar there, these actions lead to changes in the box ( memory
), these are called read and write operations. Got it?
- Release the memory

Release the memory that is not needed anymore - Throw away the chocolate wrappers.
Note: Most of the issues in memory happen here, such as memory leaks, more about that in the article on Garbage collection.
In low-level languages the developer must ensure the unused memory is de-allocated, to ensure that we do not run out of memory while executing our program, but in high-level programming languages, they embed a chunk of software called the "garbage collector" to help automatically release the memory.
While this works great for most of the part, it is very important to ensure that no unused memory stays even when not being in use, this is called a memory leak. The garbage collection works on say "approximation" since it is impossible to determine what is still needed and what is not in huge chunks of code, hence there are restrictions in the garbage collector.
The garbage collector cleans up the memory that is not in use, such as a variable that is not in scope anymore, such as one declared inside a function that is done executing, etc.
Let's proceed to discuss more about the Garbage collector in the next blog! Buckle up and I will see you there.