Memory Leak (內(nèi)存泄漏)
Origin: https://aleen42.gitbooks.io/p...
Date: Sep, 10th, 2016
In the process of developing front-end application, like web application, "memory leak" means that a space created before cannot be used or collect any more until the browser is closed.
Though the browser has GC (Garbage Collection), but there is bug on this collection, which will lead to memory leak case.
Here we list some cases that will cause memory leak in JavaScript:
Event ListenerWhen a DOM has changed, if its event listener has not be removed, IE will not handle it for you and it causes memory leak.
In such kinds of cases, you can solve by remove the listner:
Or use event delegation(事件委託):
DOM or ActiveX Object Reference
In ECMAScript, reference between two DOM elements can be collected, if no other elements refer to them like:
var a = document.getElementById("xxx"); var b = document.getElementById("xx"); /** can still be collected */ a.reference = b; b.reference = a;
However, in IE, if you loop to refer to any DOM or ActiveX Object, GC won"t collect and release these elements.
var a = document.getElementById("xxx"); a.reference = a;Closure
Closures will sometimes cause memory leak by holding a variable.
function bindEvent() { var obj = document.getElementById("xxx"); obj.onclick = function () { /** will keep holding obj even if it"s a empty function */ }; }
For this situation, we can solve it by defining handling event outside the container:
function clickHandler() { /** handler for click event */ } function bindEvent() { var obj = document.getElementById("xxx"); obj.onclick = clickHandler; }
Or directly release by deleting:
function bindEvent() { var obj = document.getElementById("xxx"); obj.onclick = function () { /** empty function */ }; /** delete this reference */ obj = null; }Delete loosely
Sometimes, if you delete an object loosely, it will result in memory leak:
var a = { p: { x: 1 } }; var b = a.p; delete a.p; console.log(b.x); /** still output => 1 */
Therefore, when deleting an object, remember to delete recursively.
var a = { p: { x: 1 } }; var b = a.p; function deleteObj(obj) { if (_.isObject(obj)) { delete obj; for (var i in obj) { deleteObj(i); } } }Boxing?
According to some documents, the following code will result in memory leak in IE, because IE will temporary create a String obj when accessing the property length, which will have a leak:
var s = "abc"; console.log(s.length);
Therefore, before we access any property of a value object, we are recommended to convert them before:
var s = "123"; console.log(new String(s).length);Illegal DOM Operations?
Some illegal DOM operations will result in memory leak in IE, like we call appendChild of a DOM element, which is not in the DOM tree.
Pollution of the global namespaceUndefined variable will be defined in the global name-space, which is another memory leak:
function foo() { bar = "this is a hidden global variable"; }
Actually:
function foo() { window.bar = "this is a hidden global variable"; }
More worse:
function foo() { this.bar = "potential global"; } foo();
Therefore, we can use the strict mode to limit our code:
/** * any acident global variable will thorw out an error * in the strict mode */ "use strict";
文章版權(quán)歸作者所有,未經(jīng)允許請勿轉(zhuǎn)載,若此文章存在違規(guī)行為,您可以聯(lián)系管理員刪除。
轉(zhuǎn)載請注明本文地址:http://specialneedsforspecialkids.com/yun/90951.html
摘要:轉(zhuǎn)發(fā)自 轉(zhuǎn)發(fā)自 http://netty.io/wiki/referenc... Since Netty version 4, the life cycle of certain objects are managed by their reference counts, so that Netty can return them (or their shared resources)...
摘要:一般情況下,的垃圾收集器會(huì)被用于檢測上面這樣的循環(huán)引用,并刪除掉它們。你可以通過強(qiáng)制垃圾收集器運(yùn)行,并檢查列表里有什么來驗(yàn)證上述結(jié)論。 -- [since Python 3.4, circular references are handled much better](http://engineering.hearsaysocial.com/2013/06/16/circular-re...
摘要:垃圾回收內(nèi)存管理實(shí)踐先通過一個(gè)來看看在中進(jìn)行垃圾回收的過程是怎樣的內(nèi)存泄漏識(shí)別在環(huán)境里提供了方法用來查看當(dāng)前進(jìn)程內(nèi)存使用情況,單位為字節(jié)中保存的進(jìn)程占用的內(nèi)存部分,包括代碼本身?xiàng)6选? showImg(https://segmentfault.com/img/remote/1460000019894672?w=640&h=426);作者 | 五月君Node.js 技術(shù)棧 | https:...
摘要:積少成多,最后造成內(nèi)存泄漏。前端內(nèi)存泄漏的影響,都是發(fā)生在客戶機(jī)器上,而且基本上現(xiàn)代瀏覽器也會(huì)做好保護(hù)機(jī)制,一般自行刷新之后都會(huì)解決。但是,一旦后端繪制內(nèi)存泄漏造成宕機(jī)之后,整個(gè)服務(wù)器都會(huì)受影響,危險(xiǎn)性更大,搞不好年終獎(jiǎng)就沒了。 引言 Memory Leak 是最難排查調(diào)試的 Bug 種類之一,因?yàn)閮?nèi)存泄漏是個(gè) undecidable problem,只有開發(fā)者才能明確一塊內(nèi)存是不是需...
摘要:假如沒有此時(shí)會(huì)進(jìn)行優(yōu)化把不會(huì)被任何閉包用到的變量從詞法環(huán)境中去掉從而消除內(nèi)存泄漏。良好的編碼方式了解一下常見現(xiàn)象可以減少內(nèi)存泄漏現(xiàn)象產(chǎn)生同時(shí)在由于失誤產(chǎn)生泄漏時(shí)保持清醒的思路借助進(jìn)行分析定位。 引言 內(nèi)存泄漏一般是由于我們編碼缺陷導(dǎo)致的,首先明確一下內(nèi)存泄漏的定義,就是應(yīng)用程序不需要,但是又不能返回給操作系統(tǒng)以供重新分配使用,導(dǎo)致可用內(nèi)存越來越少的現(xiàn)象。下面總結(jié)一下在browser端內(nèi)...
閱讀 4391·2021-11-19 09:59
閱讀 3319·2021-10-12 10:12
閱讀 2631·2021-09-22 15:25
閱讀 3321·2019-08-30 15:55
閱讀 1183·2019-08-29 11:27
閱讀 1463·2019-08-28 18:06
閱讀 2736·2019-08-26 13:41
閱讀 2554·2019-08-26 13:41