In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
Most people do not understand the knowledge points of this article "what is the principle of js garbage collection mechanism?", so the editor summarizes the following content, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "what is the principle of js garbage collection mechanism" article.
Preface
Most languages provide automatic memory management mechanisms, such as C #, Java, and JavaScript. The automatic memory management mechanism is the garbage collection mechanism we often hear about. It's amazing that language can collect garbage, , but the garbage here is not the food waste at home, but the memory occupied by some variables that are no longer in use. Our JavaScript execution environment automatically collects the garbage, that is, releases the memory occupied by variables that are no longer in use, and the garbage collection process periodically performs garbage collection at regular intervals.
Memory leak
What if the memory occupied by variables that are no longer in use is not freed? That would cause a memory leak.
What is a memory leak? Don't worry. Look down.
Memory leak is actually the heap memory that has been dynamically allocated in our program, which has not been released for some reasons, resulting in serious consequences such as the waste of system memory, slowing down the program and even crashing the system. The consequences are very serious. Now we know why there is a garbage collection mechanism.
Garbage collection mechanism
Take a look at this code to deepen your understanding of the garbage collection mechanism, ☺.
Let name =''; function aboutMe () {let hobby = 'eat meat'; let say= `I am ${name}, I like ${hobby} `; console.log (say);} aboutMe ()
When the function aboutMe () is executed, two local variables, hobby and say, are declared. After the function is executed, these two local variables will no longer be used, so the garbage collection mechanism will clear (free their memory) the (local) variables hobby and say that are no longer used.
There are two ways to implement such garbage collection in JavaScript: tag cleanup and reference counting
Mark clear
Tag cleanup is the most commonly used garbage collection method in js. Its principle is also easy to understand, don't say much nonsense, go to the code first.
Function speakLines () {let night= "dark"; / / make a mark, enter the environment let closeEyes= "close your eyes"; / / make a mark, enter the environment let speak= `to start werewolf killing, ${night} please ${closeEyes}`; / / make a mark, enter the environment console.log (speak);} speakLines () / / the marked variables in the code are marked to leave the environment and finally be recycled
When the code is executed in an environment (such as the speakLines function above), each time a variable is declared, the variable is marked (for example, to enter the execution environment). When the code execution enters another environment, that is, to leave the previous environment (after the speakLines function is executed, to execute other functions), make a tag to the variables in the previous environment (for example, mark one to leave the execution environment). When garbage collection is executed, it will decide which variables to clear to free memory.
Reference count
Reference counting is a less common form of garbage collection. It is also the principle of putting on the code before talking about the principle.
Let skill = ["singing", "playing guitar", "broadcasting"] function change () {let new_skill = ["vue", "react", "node", "webpack", ".net", "mysql", "sqlServer", "nginx"]; skill = new_skill / / A young artist becomes a technical otaku} change () console.log (skill) / / return ["vue", "react", "node", "webpack", ".net", "mysql", "sqlServer", "nginx"]
Change () declares a local variable new_skill internally and assigns an array of reference types to it, while assigning the variable new_skill to the global variable skill. In this case, the local variable new_skill will not be treated as garbage collection. Ah, why? Remember that the garbage collection we mentioned above reclaims those variables that are no longer in use and frees their memory, because the local variable new_skill is no longer a local variable that is no longer used, it is referenced by the global variable skill (still in use), so it will not be collected.
The above process uses garbage collection with reference counting.
The strategy of reference counting is to track the number of times each value is used. When a variable is declared and a reference type is assigned to it, the number of references to that value is increased by 1. If the value of the variable becomes another, the number of references to this value is subtracted by 1. When the number of references to this value becomes 0, it means that no variable is in use and the value cannot be accessed. Therefore, the space it occupies can be recycled. When garbage collection, the references with 0 times will be recycled, and the corresponding memory will be released.
Let skill = ["singing", "playing guitar", "broadcasting"] function change () {let new_skill = ["vue", "react", "node", "webpack", ".net", "mysql", "sqlServer", "nginx"]; / / 0 skill = new_skill; / 1} change () console.log (skill) management memory
In our project, we definitely want to use less memory to make the page have better performance. At this time, we need to manage memory manually and release references to the data in a timely manner. We can just store the data we need into variables. Once this data is no longer used, we need to manually assign a value to the variable null to release the reference to the data. (dereferencing) most of the things that require us to dereference manually are global variables, because local variables are automatically cleared when they leave the environment.
Let skill = ["singing", "playing guitar", "broadcasting"] function change () {let new_skill = ["vue", "react", "node", "webpack", ".net", "mysql", "sqlServer", "nginx"] skill = new_skill; / / A young man of literature and art has thus become a technical nerd} change () console.log (skill) skill = null / / assign unll to variables that are no longer used to release data references
The local variable new_skill declared inside change () is referenced by the global variable skill, so the reference number of the variable new_skill is 1. In order to make the variable new_skill clear the reference, in the last line of the code, assign a null to the global variable skill, manually dereferencing the variable skill to the variable new_skill, at this time, the reference number of the variable new_skill is minus 1, so the current reference number of the new_skill is 0. When the garbage collection mechanism is executed, when it is found that the number of references to new_skill is 0, the variable is cleared as a useless variable, freeing memory and improving performance.
The above is the content of this article on "what is the principle of js garbage collection mechanism". I believe we all have a certain understanding. I hope the content shared by the editor will be helpful to you. If you want to know more about the relevant knowledge, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.