当前位置:网站首页>Garbage collection mechanism
Garbage collection mechanism
2022-06-25 14:12:00 【Rabbit^-^】
Garbage collection
js With automatic garbage collection mechanism , There is no need to manually clear
Clearly marked
principle : When js When declaring a variable in , Mark variables as “ Enter the environment ”, Then the variable occupies a position in memory , When the variable is executed , It will be marked as “ Leaving the environment ”js The garbage collection mechanism detected “ Leaving the environment ” Will automatically recycle
This recycling method is commonly used in standard browsers , But the time interval is different
Reference count
principle :js It will track the number of references of each variable , When variables are declared , And assign a value to the variable , Then the reference count will be labeled 1, If the variable execution element or has been assigned , Then the reference count is added 1, contrary , Assign variables to other variables , Or no more operations , Then the number of times is reduced 1, Until the number becomes 0, Recycle and destroy
If a circular reference occurs ,a call b,b call a, Then the memory will not be released , Prone to memory leaks
The difference between memory leak and overflow
Memory leak : Dynamically allocate space to memory , After use , No release , Until the end of the program
out of memory : Regardless of the data size allocated by the stack , Write too much data to memory , Cause the data to cross the boundary
Stack and heap destruction
Stack memory
Global variables : Only when the browser is closed will it be recycled
local variable : Only those defined in the function are local variables , Function will produce its own scope , When the function is finished ,js The memory mechanism will automatically release
Heap memory
Object or function opens up space in heap memory , Heap memory will generate a reference address , If a code references this address , Then the object will not be destroyed .
If you want to destroy , You need to set the reference to null that will do
for example :
varx=new Array()
x=null// Destroyed
Special cases of not being destroyed
- Function execution time , Returned an object , And call... Outside the function , Private variables will not be destroyed
function fun(){// Extend scope chain
var x=[1,2,3]
return x
}
var arr=fun()
console.log(arr)// Arrays can be accessed within functions , Will not be destroyed
- In a private scope , to dom Element binding events , This private scope is not destroyed
var btn=document.getElementById("btn")
btn.οnclick=function(){
var num=11
alert(num)
}
Closure ( Special scope )
Closures are functions that have access to private variables inside other functions , It can be understood as function nesting function .
The following characteristics must be met , Be short of one cannot :
- Function nested function
- Internal functions access variables of external functions
- Variables are not recycled by the garbage mechanism
for example :
function fun(){
var a=22
console.log("fun")
return function (){
var b=1
return ++a+(++b)
}
}
var f=fun()
var v2=f()
console.log(v2)// Output 14
var v3=f()
console.log(v3)// Output 15
Be careful :fun Only once ,a Not destroyed , Closure execution multiple selection ,b Each execution is reassigned to 1
The function of closures :
- You can simulate private methods
- Used to implement the encapsulation of objects
- You can access the cache with closures ( There is a local ), When code execution takes too long , You can read from the cache first
边栏推荐
- Mysql database compressed backup_ Summary of MySQL backup compression and database recovery methods
- shell 变量 入门
- Go--- route filter
- Data acquisition system gateway acquisition plant efficiency
- 中国电池技术取得重大突破,日韩美都落后了,中国巩固了领先优势
- Hash table, hash conflict
- Deeply understand the mathematics behind deep neural networks (mysteries of neural networks Part I)
- [world history] Episode II: Dawn of civilization
- 多臺雲服務器的 Kubernetes 集群搭建
- 【开源鸿蒙系统展示】RK3568开发板搭载OpenHarmony 3.1 Release
猜你喜欢

第一次读 “Clean” 系列,并没有觉得这是一本多好的书

Renix Perf: IP网络性能测试工具及测试用例参数详解

权益NFT开创者Hash Eagle如何重新定义NFT,用权益赋能长续价值?

JVM 用工具分析OOM经典案例

NVM installation and use tutorial

当了六年程序员第一次搞懂微服务架构的数据一致性,真不容易

Logistic Regression VS Linear Regression

“移动云杯”算力网络应用创新大赛火热报名中!

Renix perf: detailed explanation of IP network performance test tools and test case parameters

Experts' suggestions | 8 measures to accelerate your innovative career planning and growth
随机推荐
Logistic Regression VS Linear Regression
What if the CPU temperature of Dell computer is too high
Shell string variable
通达信股票账户开户安全吗
国信证券股票账户开户安全吗?请问。
权益NFT开创者Hash Eagle如何重新定义NFT,用权益赋能长续价值?
shell 字符串变量
Explain the possible memory leaks caused by the handler at one time and the solutions | the developer said · dtalk
None of the MLIR Optimization Passes are enabled (registered 2)解决办法
Deploy eve-ng with KVM virtualization
Cool assembly tutorial - concise x86 assembly language tutorial (1)
用NumPy实现神经网络(Mysteries of Neural Networks Part III)
程序员为什么要软一点?
广发易淘金和同花顺哪个更好,更安全一些
请问通达信股票开户是安全的吗?
【世界历史】第一集——石器时代的人们
第一次读 “Clean” 系列,并没有觉得这是一本多好的书
Dialogue: recommended system quick start route and summary of knowledge points
go---- mgo
Discriminative v.s.Generative