


Introduction and understanding of Javascript garbage collection mechanism_Basic knowledge
People who often use Javascript will think about its garbage collection mechanism. Javascript does not require developers to manually clear garbage like C and C. When writing Javascript programs, developers do not need to worry about memory usage, required memory allocation and The recycling of useless memory (garbage) is completely managed automatically. The root cause is that the program collects variables that are no longer used and releases the memory they occupy. Therefore, the garbage collection mechanism will perform this operation periodically and repeatedly at fixed intervals.
For example, local variables only exist inside the function. The program will allocate corresponding storage space for local variables in stack memory or heap memory. When the function ends, the memory occupied by the local variables no longer exists. When necessary, the program will release the memory occupied by the local variable for use by other variables. This is the simplest way for a program to release memory, but many times, variables in the program will be used all the time. At this time, the garbage collection mechanism must track the variable and determine whether it is used and whether its memory space can be released.
The garbage collection mechanism mainly judges variables to release memory space in two ways: one is the mark and clear method, and the other is the reference counting method.
notation, each variable has its own running environment. After the variable is created, it will run in a certain environment. For example, if you create a local variable, the local variable will run in the function body. When the function is running, the local variable will be marked as "entering the environment". When the function body ends, it means that the variable leaves its running environment. At this time, the variable will be marked as "leaving the environment". For variables that "leave the environment", the garbage collection mechanism will record them accordingly and release them in the next collection cycle.
Reference counting method, tracking the number of times each value is referenced. When you declare a variable and assign a reference type value to the variable, the value has a reference count of 1. If the same value is assigned to another variable, the reference count of the value is increased by 1. Conversely, if the variable containing a reference to this value takes another value, the value's reference count is decremented by one. When the number of references to this value is 0, it means that there is no way to access this value anymore, so the memory space it occupies can be recycled. When the garbage collector runs in the next cycle, the memory space occupied by the value with zero reference count is released. (Original explanation reference: Javascript Advanced Programming - Second Edition)
For example:
After this function exits, the counter reading of object1 is 1, and the counter degree of object2 is 1. So both variables will not be destroyed. If a large number of such programs exist in the function body, a large amount of memory will be wasted and cannot be recycled, resulting in memory leaks.
To solve the above problem, manually release the memory occupied by object1 object2. That is:
Copy code
The code is as follows:
Copy code
The code is as follows:
function countMethod(){
var object1 = new Object(); // Declare variable, counter changes from 0 to 1
var object2 = new Object(); // Declare variable, counter From 0 to 1
object1.method1 = "This is object1"; // object1 counter -1, object1 reading changes to 0
object2.method2 = "This is object2"; // object2 counter -1, The reading of object2 becomes 0
}
As can be seen from the above example, under normal circumstances, when the function ends, the reading of object1 and object2 is 0. In the next garbage collection cycle, it will is recycled and the memory it occupies is released.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Hardwood is an important synthetic material in Stardew Valley. We can have many uses in the game, so we can stock it up in our daily life. What are the specific ways to obtain hardwood? Below we will bring you Stardew Valley. You can refer to the way to obtain hardwood in Monogatari if necessary. Ways to obtain hardwood in Stardew Valley 1. Go to the secret forest every day to dig tree stumps to quickly obtain hardwood. 2. There is a chance to obtain mahogany seeds by mining tree stumps and fighting monsters in the secret forest. 3. Planting seeds in the yard will produce mahogany trees. 4. Finally, after mining mahogany, you can quickly obtain a large amount of hardwood.

Preface As keen Linux users, we often encounter the need to install CentOS or CentOS7 on mobile phones. Although mobile phones are not the best Linux running platform, sometimes we need to perform some Linux-related operations or development work on mobile phones. In this article, we will discuss in detail how to install CentOS or CentOS7 on your phone. Installing CentOS on a mobile phone To install CentOS on a mobile phone, we first need a mobile phone that supports virtualization technology, such as a mobile phone that supports Android system. Then we can use a terminal emulator application such as Termux to simulate the Linux environment. In Termux, we can Use a package manager to install CentOS

Microsoft has added a new feature to the Edge browser on Windows 11/10 in the latest stable version, namely "Mobile Upload". Users can now upload files directly from their phones through the Edge browser, and the feature works on almost all websites without file format restrictions. The introduction of this feature provides users with a more convenient upload method, making it more efficient to manage and share files when using the Edge browser. How to use the "Mobile Upload" feature: Open the Edge browser, click the "Upload" option on any website, and select "Upload from mobile" in the file picker. Use your phone camera to scan the QR code. Click "Confirm" to pair the device in the Edge browser. Click on the "Upload File" option

Title: An in-depth exploration of the storage location and mechanism of Golang variables. As the application of Go language (Golang) gradually increases in the fields of cloud computing, big data and artificial intelligence, it is particularly important to have an in-depth understanding of the storage location and mechanism of Golang variables. In this article, we will discuss in detail the memory allocation, storage location and related mechanisms of variables in Golang. Through specific code examples, it helps readers better understand how Golang variables are stored and managed in memory. 1.Memory of Golang variables

CSS reflow and repaint are very important concepts in web page performance optimization. When developing web pages, understanding how these two concepts work can help us improve the response speed and user experience of the web page. This article will delve into the mechanics of CSS reflow and repaint, and provide specific code examples. 1. What is CSS reflow? When the visibility, size or position of elements in the DOM structure changes, the browser needs to recalculate and apply CSS styles and then re-layout

When some users use computers, Win10 system cleans up junk. When encountering Win10 system cleaning up junk, how should we solve it? Nowadays, there are still many users who don’t know how to deal with the situation of cleaning up garbage in Win10 system, so let’s take a look at the step-by-step tutorial on cleaning up system junk in Win10. Let’s take a look. 1. On the computer desktop, click "This PC" and manage. 2. Select Disk Management 3. Select the disk you want to clean, right-click and select Properties. 4. Click the "Disk Cleanup" button at the bottom of the page. 5. In the interface that appears, select the files you want to clean, click "OK", and click "Delete Files". The above is a step-by-step tutorial on how to clean up system junk in Windows 10. I hope

As the PHP language becomes more and more popular, developers need to use more and more classes and functions. When a project grows in size, manually introducing all dependencies becomes impractical. At this time, an automatic loading mechanism is needed to simplify the code development and maintenance process. The auto-loading mechanism is a feature of the PHP language that can automatically load required classes and interfaces at runtime and reduce manual class file introduction. In this way, programmers can focus on developing code and reduce errors and time waste caused by tedious manual class introduction. In PHP, generally

In Genshin Impact, players will face many challenges, and each challenge mission brings different fun. However, it is also difficult to successfully complete these challenges and collect the lost immortal power. Here's how to accomplish this task. First of all, players need to explore every corner of the game world to find the scattered fairy power. These magical powers may be hidden in valleys, caves, or other secret places. Use your character's skills and abilities to help you discover them. Secondly, teaming up with other players is a great way to complete missions. Exploring in a group can increase your chances of finding Xianli and complete the task quickly. Team up with friends or other players to explore the world together, assist each other, and collect more fairy powers. Finally, beware of enemies and traps. How to complete the process of collecting scattered immortal power in Xun Yuanshen
