Why does the front end care about memory
- Prevent excessive memory usage, resulting in page lag, or even no response
- Node.js uses the V8 engine, and memory management is critical for the server. Because of the persistence of the server, memory is more likely to accumulate and cause memory overflow
Js garbage collection mechanism
Js uses the garbage collection mechanism to automatically manage memory, which has obvious advantages and disadvantages.
- Advantages: Can greatly simplify the memory management code in the application, reducing the burden of developers, but also reduce the memory leakage problem caused by long running
- Disadvantages: means developers have no control over memory management, we can’t force them to do garbage collection and manage it
Here are a few garbage collection strategies for JS:
Reference counting
IE8 is mainly used by browsers below, modern browsers have abandoned this way, here is only a brief introduction.
The basic principle is that the record keeps track of the number of times each value is referenced. The number of references is increased by one, the number of references is decreased by one, and when the value is zero, the memory occupied by the changed value is freed.
Mark clear
Major browsers use garbage collection.
When a variable enters the environment (for example, declaring a variable in a function), the variable is marked as “entering the environment.” Logically, you can never free up memory occupied by variables that enter the environment, because they may be used whenever the execution stream enters the corresponding environment.
When a variable leaves the environment, it is marked as “out of the environment.” Variables can be marked in any way. For example, you can flip a particular bit to record when a variable entered the environment, or use a list of variables that entered the environment and a list of variables that left the environment to track which variable changed.
function test(){
var a = 10; // Marked "enter environment"
var b = "hello"; // Marked "enter environment"
}
test(); // After execution, a and B are marked "out of the environment" and recycled
Copy the code
At the end of the day, it doesn’t really matter how you mark variables, but what strategy you use. At run time, the garbage collector marks all variables stored in memory (of course, it can be marked in any way).
It then unflags variables in the environment and those referenced by variables in the environment. Variables tagged after this point are considered to be ready for deletion because they are no longer accessible to variables in the environment.
Finally, the garbage collector completes the memory cleanup, destroying the tagged values and reclaiming the memory they occupy.
The environment can be understood as our scope, but globally-scoped variables are destroyed only when the page is closed.
V8 Memory control
V8 memory limits
A Node can only use part of its memory, 1.4GB on a 64-bit system and 0.7GB on a 32-bit system. Under this limit, Node will not be able to manipulate large memory objects, such as reading a 2GB file into memory, even though it has 64 GB of physical memory. In this case, we can use the Buffer class to complete the reading of large memory files.
The main reason for this is that Node is built on V8, and V8’s memory management mechanism, which is mainly used in the browser, can meet all the needs of the front-end page, but in Node, it limits the ability of developers to use as much memory as they want.
V8’s garbage collection mechanism
V8’s garbage collection strategy is based on generational garbage collection. The main memory is divided into the new generation and old generation of two generations.
Young Generaion
Features:
- The managed object has a short lifetime
- The space occupied is much smaller than the old generation space
- Garbage returns are particularly frequent
The Scavenge algorithm is used to recycle garbage in Cenozoic space, which works as follows:
- The new generation space is divided into two Spaces, called Semispace. The space in use is called From space, and the space in idle is called To space. When we allocate objects, we first allocate objects in From space.
- When garbage collection begins, live objects in the From space are checked, copied To the To space, and memory in the From space is freed.
- From space swaps with To space
As we can see from the above procedure, the Scavenge algorithm is typically an algorithm that trades space for time. The disadvantage is that you can only use half of the heap memory, and the advantage is that you have an excellent performance in time efficiency.
OldGeneraion space
Objects with a long life span in the new generation space are copied into the old generation space, a process called promotion. There are two main conditions for the promotion of objects:
- Whether the object has been screcycled. When an object is copied From the From space To the To space, its memory location is checked To determine whether the object has been screcycled. If so, copy it directly To the old space, not To space.
- Whether the memory usage of the TO-space exceeds 25% of the to-space. Description When an object was copied From the From space To the To space, the memory usage of the To space exceeded the limit. Since the To space will become the From space, in order not To affect the subsequent memory allocation, will be directly promoted To the old space.
The Scavenge avenge presents two problems due to the large amount of applications.
- If there are many live objects, the efficiency of copying live objects is low
- It is wasteful to split two Semispace Spaces
For this reason, the old generation mainly adopts the combination of mark-sweep and mark-compact for garbage collection, and its working principle is as follows:
- In the mark phase, all objects in the heap are iterated and marked as living objects, and in the subsequent purge phase, only unmarked objects are cleared. The schematic diagram after marking is as follows:
Yellow is marked as active and dark gray is marked as dead.
The biggest problem with Mark-sweep is that memory is discontinuous after a marked dead object is cleared, and this fragmentation space can cause problems with subsequent memory allocation.
- In order to solve the problem of memory fragmentation, mark-compact was proposed. It moves the living object to one end after the tag is complete, and then frees up space beyond the living object’s end.
Incremental Marking
For the V8 garbage collection procedures, need to suspend application logic, but because of the old generation space is very large, and many live objects, in order to avoid a long pause, will be a one-time operation instead of incremental tag, originally is broken up into many small “step”, haven’t finished a “step”, let the application logic to perform for a while, alternate with, Until the garbage collection execution is complete.
Reference article:
- Javascript Advanced Programming
- Node.js