
There’s a lot to learn about how GC works. There are several ways to monitor GC activity and the increase of the ELD (event loop delay) is just one of the available approaches. In prior Node.js versions, the GC was prone to generate bottlenecks in the application due to misuse by the user. JS Engines put a lot of effort into making GC efficient. Nevertheless, when an application is allocating and freeing big blocks of memory it may lead to a block in the event loop. Nowadays, the V8 garbage collection is really efficient. This is because of the GC, as explained in the last section. In Node.js (or specifically V8), it’s more efficient to frequently allocate small short-lived objects, rather than modifying large long-lived objects. So, just because memory usage is never decreasing, this doesn’t necessarily mean there’s a memory leak. V8 prefers allocating more heap rather than collecting from old space. For this reason, collecting from old space is slow. This phase is expensive because V8 needs to move objects around. In this situation, the thread will only mark those blocks to be freed in another thread. This approach is also called mark-and-sweep. This means that, in any Node.js application, there’s a thread scanning the old space looking for a memory address that isn’t reachable, which also means that it can be freed. The GC handles some threads behind the scenes and one of them is to mark blocks of memory to be freed. In this section, we’re going to discuss old space memory management. In the last section, we discussed how V8 memory is divided and how it handles the new space allocation. For more details, I strongly suggest reading the V8 documentation. Collecting memory from the Old SpaceĪs mentioned above, the V8 Garbage Collector is complex this article aims to show the major features from a broader perspective. These flags are -expose-gc and -trace-gc respectively. It also provides a way to trace what’s happening in GC. Node.js provides an API to control the GC from the JavaScript side. However, it’s important to mention that, when an object from old space is accessed through to space, it loses the cache locality of your CPU and it might affect performance because the application is not using CPU caches. It’s copied to old space! When an object is moved from the new space to the old space, it’s fully copied, which is an expensive operation.Įven though it’s an expensive operation, the GC is fast enough to do it unnoticeably.
#STACK VS HEAP JAVASCRIPT FREE#
For this reason, it’s a good idea to clear the objects as soon as possible to free up memory for new objects and avoid them being allocated in the old space.

While the allocation in the new space is very cheap, the new space is also fairly small in size (between 1 and 8MB).

New space: most objects are allocated here.The old space can be controlled by the flag -max-old-space-size

Usually, objects are moved here after surviving in a new space for some time. Old space: where older objects are stored.The Memory Heap is divided into two major spaces:
#STACK VS HEAP JAVASCRIPT FULL#

