We all know that garbage collection (GC) is important for modern application development. It depends on your programming language and many developers have little idea how it is done.
The reason we need GC is because of the memory usage during programming. You create functions, objects, etc. that take up memory space.
One difference with a stack is that this is a data structure used to literally stack elements, such as references to real objects from raw data. The stack allocation policy is “safer” because it knows how much memory is allocated and is fixed.
Consider the following code example.
The following is an explanation of points 1-3 in the code example above.
- simply create a new original variable using a string value.
Reference Counting GC
As the name suggests, this strategy works by searching through memory for resources that have zero references pointing to them.
Let’s look at the previous code snippet as a reference to get a better understanding.
This is an object that has multiple internal properties
task. Then let’s assume that another developer decides that task can simply use the original language representation
task = 'Walk the dogs'. So, the first
task object no longer points to its reference, which makes it available for GC.
This sounds simple, but in practice there is much more to it than that.
This may not represent a functional task in a real application, but it is sufficient to imagine a situation where the internal properties of two objects refer to each other.
Tagging and scanning algorithms
The tag-and-scan algorithm is known for its use in many programming languages for garbage collection. In short, it makes use of intelligent methods to determine if a given object can be reached from the root object.
global object; if you are on a browser, it is the
The algorithm starts at the top, drops down the hierarchy, and marks each object in turn that is reachable (i.e. - still referenced) and cleans up those objects from the root object that are not scanned.
node.js how to free memory
Let’s take a look at the following representation.
The Node.js heap is divided into two main parts: the new space and the old space. The former is where new objects are allocated, while the latter is where they are stored for the long term.
As a result, garbage collection of objects in the new space is faster than in the old space. On average, it takes up to 20% of the objects referenced to get into the old space.
Because of this feature, V8 makes use of an additional GC strategy: scavenger.
As we have seen, it is more expensive for Node to release things in old space. When it has to do so, the mark and scan algorithm runs to achieve the goal.
The scavenger GC specializes in collecting garbage from new generations. Its strategy consists in selecting long-lived objects and moving them to the so-called old space. To achieve this step, V8 ensures that at least half of the new generation remains empty; otherwise, it faces the problem of lack of memory.
The idea is to keep track of all references to the younger generation without having to go through the entire older generation. In addition, Scavenger points a set of references from the new space to the old space of the object.
Please keep in mind that, as with many other languages, we cannot be sure of GC runs. Since 2019, it depends on the GC, from time to time, to perform cleanups that you cannot trigger yourself.