javascriptThe column introduces the garbage collection mechanism, memory leaks, and closure contents. Let’s take a look at the fast-end bench.
Write at the front: This is a series I am about to start writing in the javascript column, mainly in the framework of rampant In the era, although I use a framework for work, for interviews and technical advancement, the basic knowledge of JS is the icing on the cake, and it is also a piece of knowledge that I have to learn. Although you don’t need to know a lot about cars to drive a car, you only need to master the common uses of cars. Just function. But if you know cars, you can drive better, and by the same token. Of course, an article will not just talk about one knowledge point. Generally, related knowledge points will be connected in series. While recording your own learning, you will share your own learning and encourage each other! If you can, please also please give me a like, your likes will also make me work harder to update!
Overview
Eating time: 6-12 minutes
Difficulty: Easy, don’t run, watch before leaving
Garbage collection mechanism
The previous blog mainly explains the allocation and use of memory (stack memory and heap memory, deep copy and shallow copy). After use, of course, Returning unused memory is like clearing unused software on your phone from the background, which can improve the running speed of your phone. Otherwise, if more and more memory is used, it will get stuck sooner or later. The same goes for JS.
Every once in a while, the garbage collector of JS will "patrol" the variables, just like security patrols in the park, letting irrelevant people leave quickly . When a variable is no longer needed, it will release the memory space occupied by the variable. This process is called Garbage Collection
JS Garbage There are two types of recycling algorithms, reference counting and mark clearing.
Reference counting method
The reference counting method is the most basic garbage collection algorithm and has been used by modern browsers. The equipment has been eliminated. Before learning the reference counting method, you need to first have a certain concept of reference. You can think of it as a description of the memory address pointed to by the current variable, which is somewhat similar to the memory pointer of the JS reference data type. To understand the concept, let’s first look at a line of code:
var obj={name:'jack'};复制代码
When we assign a value to obj, we actually create a reference pointing to the variable, reference count is 1, Under the mechanism of reference counting, each value in the memory will correspond to a reference count
, and when we assign a value to obj, it is When null, this variable becomes a piece of useless memory. At this time, the reference count of obj will become 0, and it will be garbage collected. The memory space occupied by obj will be released
We know that the life cycle of the function scope is very short. After the function is executed, the memory space inside Variables are basically useless variables. The consequence of not clearing them is that the memory garbage is not released, and it still occupies the original memory and does not let go, which will easily cause memory leak. Let’s first look at a piece of code and Running results:
function changeName(){ var obj1={}; var obj2={};
obj1.target=obj2;
obj2.target=obj1;
obj1.age=15; console.log(obj1.target); console.log(obj2.target);
}
changeName();复制代码
##We can see that obj1.target and obj2.target have mutual references. situation, because when obj1.age is changed, obj1.target.age and obj2.target.age are also affected at the same time, and they point to The reference count is consistent When the function is executed,
obj1 and obj2 are still alive and well, because obj1.target and ## After the execution of #obj2.target is completed, the reference count is still 1. It is clear that the function has been executed, but this kind of garbage still exists. There are too many such functions defined, Memory leakIt will also be unavoidable
Mark clearing method
The disadvantages of the above reference counting method are already obvious, so now we are going to talk about the marking clearing method There is no such problem. Because the judgment standard it uses is to see whether the object
can be reached
, it is mainly divided into two stages, marking stage and clearing stage:
Marking phase
The garbage collector will start from the root object (Window object) and scan all reachable objects. This is the so-called
reachable
Clear phase
While scanning, objects that cannot be reached by the root object (
unreachable
) are objects that are considered unnecessary and will be cleared as garbage
现在再来看下上面的代码
function changeName(){ var obj1={}; var obj2={};
obj1.target=obj2;
obj2.target=obj1;
obj1.age=15; console.log(obj1.target); console.log(obj2.target);
}
changeName();复制代码
The above is the detailed content of Understand the garbage collection mechanism, memory leaks, and closures of JS series (3) with one piece of paper. For more information, please follow other related articles on the PHP Chinese website!
Statement
This article is reproduced at:juejin. If there is any infringement, please contact admin@php.cn delete
Python is more suitable for beginners, with a smooth learning curve and concise syntax; JavaScript is suitable for front-end development, with a steep learning curve and flexible syntax. 1. Python syntax is intuitive and suitable for data science and back-end development. 2. JavaScript is flexible and widely used in front-end and server-side programming.
Python and JavaScript have their own advantages and disadvantages in terms of community, libraries and resources. 1) The Python community is friendly and suitable for beginners, but the front-end development resources are not as rich as JavaScript. 2) Python is powerful in data science and machine learning libraries, while JavaScript is better in front-end development libraries and frameworks. 3) Both have rich learning resources, but Python is suitable for starting with official documents, while JavaScript is better with MDNWebDocs. The choice should be based on project needs and personal interests.
The shift from C/C to JavaScript requires adapting to dynamic typing, garbage collection and asynchronous programming. 1) C/C is a statically typed language that requires manual memory management, while JavaScript is dynamically typed and garbage collection is automatically processed. 2) C/C needs to be compiled into machine code, while JavaScript is an interpreted language. 3) JavaScript introduces concepts such as closures, prototype chains and Promise, which enhances flexibility and asynchronous programming capabilities.
Different JavaScript engines have different effects when parsing and executing JavaScript code, because the implementation principles and optimization strategies of each engine differ. 1. Lexical analysis: convert source code into lexical unit. 2. Grammar analysis: Generate an abstract syntax tree. 3. Optimization and compilation: Generate machine code through the JIT compiler. 4. Execute: Run the machine code. V8 engine optimizes through instant compilation and hidden class, SpiderMonkey uses a type inference system, resulting in different performance performance on the same code.
JavaScript's applications in the real world include server-side programming, mobile application development and Internet of Things control: 1. Server-side programming is realized through Node.js, suitable for high concurrent request processing. 2. Mobile application development is carried out through ReactNative and supports cross-platform deployment. 3. Used for IoT device control through Johnny-Five library, suitable for hardware interaction.
I built a functional multi-tenant SaaS application (an EdTech app) with your everyday tech tool and you can do the same.
First, what’s a multi-tenant SaaS application?
Multi-tenant SaaS applications let you serve multiple customers from a sing
This article demonstrates frontend integration with a backend secured by Permit, building a functional EdTech SaaS application using Next.js. The frontend fetches user permissions to control UI visibility and ensures API requests adhere to role-base
JavaScript is the core language of modern web development and is widely used for its diversity and flexibility. 1) Front-end development: build dynamic web pages and single-page applications through DOM operations and modern frameworks (such as React, Vue.js, Angular). 2) Server-side development: Node.js uses a non-blocking I/O model to handle high concurrency and real-time applications. 3) Mobile and desktop application development: cross-platform development is realized through ReactNative and Electron to improve development efficiency.
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.