There is GC (garbage collection mechanism) in javascript. JavaScript is a language that uses a garbage collection mechanism. The execution environment is responsible for managing memory when the code is executed, and automatically destroys garbage objects (objects that are not referenced) from memory.
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
Garbage collection related concepts
① What is garbage
Objects that are not used (referenced) aregarbage.
② What is garbage collection
Objects that are not referenced are destroyed and the memory is released, which is garbage collection.
Programming languages such as C and C require manual garbage collection.
Automatic garbage collection for Java, JavaScript, PHP, Python and other languages.
JS has an automatic garbage collection mechanism, which will automatically destroy these garbage objects from memory. We do not need and cannot perform garbage collection operations. All we need to do is set objects that are no longer used to null.
Reference counting method
Ideas
let arr = [1, 0, 1] // [1, 0, 1]这块内存被arr引用 引用次数为1 arr = [0, 1, 0] // [1, 0, 1]的内存引用次数为0被释放 // [0, 1, 0]的内存被arr引用 引用次数为1 const tmp = arr // [0, 1, 0]的内存被tmp引用 引用次数为2
Circular reference problem
Netscape Navigator 3.0 adopts
function Example(){ let ObjectA = new Object(); let ObjectB = new Object(); ObjectA.p = ObjectB; ObjectB.p = ObjectA; } Example();
ObjectA = null; ObjectB = null;
In order to solve the memory leak problem caused by circular references, Netscape Navigator 4.0 began to use the mark and clear method
By 2008, IE, Firefox, Opera, Chrome and Safari both use markup cleaning (or a variant of it) in their JavaScript implementations, differing only in how often they run garbage collection.
Ideas
function Example(n){ const a = 1, b = 2, c = 3; return n * a * b * c; } // 标记Example进入执行上下文 const n = 1; // 标记n进入执行上下文 Example(n); // 标记a,b,c进入执行上下文 console.log(n); // 标记a, b, c离开执行上下文,等待垃圾回收
const and let declarations improve performance
Garbage collection of the V8 engine
The garbage collection of the V8 engine adopts the mark-and-sweep method and the generational collection method
Divided into new generation and old generation
New generation
##New generation garbage collection adoptsScavenge
Algorithm
Allocate to common memory and newly allocated small amount of memory
新生代 -> 老生代
老生代
老生代采用
mark-sweep
标记清除和mark-compact
标记整理
通常存放较大的内存块和从新生代分配过来的内存块
1类
2类
3类
1类
,然后进行深度优先遍历。2类
。3类
。标记完成之后,将标记为1类
的对象进行内存释放
Mark-compact
垃圾回收完成之后,内存空间是不连续的。
这样容易造成无法分配较大的内存空间的问题,从而触发垃圾回收。
所以,会有Mark-compact步骤将未被回收的内存块整理为连续地内存空间。
频繁触发垃圾回收会影响引擎的性能,内存空间不足时也会优先触发Mark-compact
垃圾回收优化
全局变量
// exm1 function Example(){ exm = 'LeBron' } // exm2 function Example(){ this.exm = 'LeBron' } Example()
未清除的定时器
const timer = setInterval(() => { //... }, 1000) // clearInterval(timer)
闭包
function debounce(fn, time) { let timeout = null; return function () { if (timeout) { clearTimeout(timeout); } timeout = setTimeout(() => { fn.apply(this, arguments); }, time); }; } const fn = debounce(handler, 1000); // fn引用了timeout
未清除的DOM元素引用
const element = { // 此处引用了DOM元素 button:document.getElementById('LeBron'), select:document.getElementById('select') } document.body.removeChild(document.getElementById('LeBron'))
这个其实不难,浏览器原带的开发者工具Performance就可以
1、尽量不在for循环中定义函数
// exm const fn = (idx) => { return idx * 2; } function Example(){ for(let i=0;i<1000;i++){ //const fn = (idx) => { // return idx * 2; // } const res = fn(i); } }
2、尽量不在for循环中定义对象
function Example() { const obj = {}; let res = ""; for (let i = 0; i < 1000; i++) { // const obj = { // a: i, // b: i * 2, // c: i * 3, // }; obj.a = i; obj.b = i * 2; obj.c = i * 3; res += JSON.stringify(obj); } return res }
3、清空数组
arr = [0, 1, 2] arr.length = 0; // 清空了数组,数组类型不变 // arr = [] // 重新申请了一块空数组对象内存
【推荐学习:javascript高级教程】
The above is the detailed content of Does javascript have gc?. For more information, please follow other related articles on the PHP Chinese website!