Example Of A Memory Leak

Here's a code sample to consider, it is contrived but bear with me:

function someFn(){

var $input = $("<input type='button' />");

$input.appendTo("body");

window.addEventListener('click', function(){

if($input.val() == ""){
// some code goes here
}

}, false);
}

 

If you were to remove $input from the DOM, it would not get garbage collected because the callback for the listener has a handle on it.

 

Likewise, be careful if you do something like this:

function someFn(){

var $someDiv = $("#someDiv");

window.addEventListener('click', function(){

if($someDiv.is(':hidden')){
// some code goes here
}

}, false);
}

 

In this case, if you ever removed $someDiv from the DOM, it would not be garbage collected. So this may be a better solution:

function someFn(){

var $someDiv = $("#someDiv");

window.addEventListener('click', function(){

if($("#someDiv").is(':hidden')){
// some code goes here
}

}, false);
}

 

In the past I would have taken the previous route because I always tried to reduce the amount of times I wrapped something in jQuery, but now I'll have to be careful about what my code is doing. If I never intend to remove $someDiv, then maybe the previous sample is OK, but if there is a possibility that I remove it, then the last sample will avoid a memory leak.

Add a Comment...

 

Comments

best crib mattress 2017 best mattress of 2017 for side sleeper best mattress in 2017 the best mattresses 2017 best mattresses in 2017 top rated mattress 2017 best mattress for 2017 best mattress in 2017 best mattresses 2017 best rated mattresses 2017 top 5 mattresses 2017 best rated mattress 2017 best rated mattress for 2017 best mattresses in 2017 consumer reports best mattress 2017 casper coupon code casper mattress promo code casper com coupon code leesa discount codes leesa coupo - 7/22/2017

Absolutely NEW update of SEO/SMM package "XRumer 16.0 + XEvil 3.0": captcha recognition of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex, and more than 8400 another subtypes of captcha, with highest precision (80..100%) and highest speed (100 img per second). You can connect XEvil 3.0 to all most popular SEO/SMM programms: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other programms. Interested? There are a lot of introducing videos about XEvil in YouT - 7/22/2017

Absolutely NEW update of SEO/SMM package "XRumer 16.0 + XEvil": captcha regignizing of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex, and more than 8400 another size-types of captcha, with highest precision (80..100%) and highest speed (100 img per second). You can connect XEvil 3.0 to all most popular SEO/SMM software: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other software. Interested? There are a lot of impessive videos about XEvil in YouTube. - 7/22/2017

Absolutely NEW update of SEO/SMM package "XRumer 16.0 + XEvil": captcha breaking of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex, and more than 8400 another subtypes of captchas, with highest precision (80..100%) and highest speed (100 img per second). You can connect XEvil 3.0 to all most popular SEO/SMM programms: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other software. Interested? There are a lot of impessive videos about XEvil in YouTube. Go - 7/22/2017

Revolutional update of SEO/SMM software "XRumer 16.0 + XEvil": captchas recognition of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex, and more than 8400 another categories of captchas, with highest precision (80..100%) and highest speed (100 img per second). You can connect XEvil 3.0 to all most popular SEO/SMM programms: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other software. Interested? There are a lot of introducing videos about XEvil in YouTub - 7/21/2017

Absolutely NEW update of SEO/SMM software "XRumer 16.0 + XEvil": captchas solution of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex, and more than 8400 another categories of captchas, with highest precision (80..100%) and highest speed (100 img per second). You can connect XEvil 3.0 to all most popular SEO/SMM programms: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other software. Interested? There are a lot of demo videos about XEvil in YouTube. Goo - 7/18/2017

Absolutely NEW update of SEO/SMM package "XRumer 16.0 + XEvil": captcha recognition of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex, and more than 8400 another subtypes of captchas, with highest precision (80..100%) and highest speed (100 img per second). You can connect XEvil 3.0 to all most popular SEO/SMM software: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other programms. Interested? There are a lot of impessive videos about XEvil in YouTube. - 7/18/2017

Revolutional update of SEO/SMM package "XRumer 16.0 + XEvil 3.0": captchas recognition of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex, and more than 8400 another types of captcha, with highest precision (80..100%) and highest speed (100 img per second). You can connect XEvil 3.0 to all most popular SEO/SMM programms: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other programms. Interested? You can find a lot of introducing videos about XEvil in YouTu - 7/17/2017

Absolutely NEW update of SEO/SMM software "XRumer 16.0 + XEvil 3.0": captcha breaking of Google, Facebook, Bing, Hotmail, SolveMedia, Yandex, and more than 8400 another categories of captchas, with highest precision (80..100%) and highest speed (100 img per second). You can connect XEvil 3.0 to all most popular SEO/SMM programms: XRumer, GSA SER, ZennoPoster, Srapebox, Senuke, and more than 100 of other programms. Interested? There are a lot of demo videos about XEvil in YouTube. - 7/14/2017

Hi there, I read your blogs like every week. Your story-telling style is awesome, keep it up! maglia real madrid 2017 - 5/16/2017

BeatrizBr Elfenbenskusten DustyPcqi Angelicap Barcelona ChristieM GlendaHar Slovakien SophiaBur MarlaPann Roma TraceerpK KerriSchu Tyskland Milagrosy - 3/08/2017

EmelyTufn Rumanien KlaraMose CharissaW Elfenbenskusten MorganAli NataliaLe Argentina CassieFre TCCRaleig Spanien RonSmpps AthenaChe Italien JacobHind NganBrack Atletico Madrid ClairSepp KeiraFair Dortmund NicolasSe Dickqkayz Olympique De Marseille OmaynBekk LolaChinn Inter Milan KimberlyM QuyenFjuh Seattle Sounders ElijahTor - 3/07/2017