Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

js内存泄漏问题

   var user = { name: 'tom', age: 20, gender: 'male' }
   var test = document.getElementById('test');
       test.onclick = function() {
           test.innerHTML = user.name;
       }
   user = null; // 释放对象
   

最近遇到一个这样的面试题,请问这段有没有内存泄漏,如果有的话麻烦大神指出为什么,并且如何消除内存泄漏


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

内存泄漏简单的说就是分配的内存既不能使用,又不能回收,直到浏览器进程结束。

对于循环引用问题直接消除引用就好,设置test=null,因为js垃圾回收机制会周期的执行,找出不再使用的变量,然后释放掉其占用的内存.
垃圾回收


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...