Error: Uncaught RangeError: Maximum call stack size exceeded
<--- Last few GCs --->
[43697:0x103800600] 342449 ms: Mark-sweep 1365.6 (1403.2) -> 1365.4 (1402.7) MB, 344.7 / 0.0 ms (average mu = 0.781, current mu = 0.000) last resort GC in old space requested
[43697:0x103800600] 342774 ms: Mark-sweep 1365.4 (1402.7) -> 1365.4 (1402.7) MB, 325.1 / 0.0 ms (average mu = 0.644, current mu = 0.000) last resort GC in old space requested
window.onload = function(){
var element = document.getElementById('example')
element.onclick= function anotherFunction(){
document.getElementById("anotherElement").innerHTML = "Processing...";
}
浏览器的垃圾回收机制
1. 为什么要了解垃圾回收机制
let b = () => { a() }
a()
Error: Uncaught RangeError: Maximum call stack size exceeded
<--- Last few GCs --->
[43697:0x103800600] 342449 ms: Mark-sweep 1365.6 (1403.2) -> 1365.4 (1402.7) MB, 344.7 / 0.0 ms (average mu = 0.781, current mu = 0.000) last resort GC in old space requested [43697:0x103800600] 342774 ms: Mark-sweep 1365.4 (1402.7) -> 1365.4 (1402.7) MB, 325.1 / 0.0 ms (average mu = 0.644, current mu = 0.000) last resort GC in old space requested
function markFromRoots(){ const worklist = [] for(let childNode in Roots){ if(childNode != null && isNotMarked(childNode)){ setMarked(childNode) worklist.push(childNode) mark() } } }
function mark(){ while(!isEmpty(worklist)){ const PointNode = worklist.pop() for(let childNode in PointNode){ if(childNode != null && isNotMarked(childNode)){ setMarked(childNode) worklist.push(childNode) } } } }
functon sweep(start,end){ while(start < end){ const Node = Stack[start] if isMarked(Node) setUnMarked(Node) else free(Node) start ++ } }
sweep(Stack.startLocation,Stack.endLocation)
let a = {} //对象{}的引用次数+1 = 1 let b = a //对象{}的引用次数+1 = 2 a = null //对象{}的引用次数-1 = 1 b = null //对象{}的引用次数-1 = 0 可以被回收了
function test(){ let a = {} let b = [] a.prop = b b.prop = a } test()
//test执行完后,a和b的引用次数都是2,不会被回收 //标记清除不会有问题,此时test执行完后,两个对象都已经离开环境,会被标记清除
window.onload = function(){ var element = document.getElementById('example') element.onclick= function anotherFunction(){ document.getElementById("anotherElement").innerHTML = "Processing..."; }
}
let something = null let someMessage = 'Hello' let replacething = ()=>{ let anotherthing = something
}
//每次replacething被调用,something都会获取一个新的对象,包含一个大数组和一个新的闭包(someMethod) //unused也保持一个闭包(anotherthing) //someMethod与unused共享作用域,即使它未使用,但是它对originalThing的引用强制它保持活动