1

Last time I research in Google for memory leaks, cycle reference and how garbage collection works. But all is little confusing. I understant that garbage collection works in this way:

var a = {}; // a is only reference to this object
a = 5 /* now variable a is reference to "number" (other js object)
and now the first empty object has no reference,
so it is cleaned from the memory. */

That look pretty cool. But I see have a JScript objects and COM objects in IE. If there is a circular reference between objects in COM and JScript there are memory leaks (uncleaned memory). So now I'm comfused and makes some simple examples to show what I understand, or not:

<!DOCTYPE html>
<html>
<head>
    <title></title>
</head>
<body>
    <div style="width: 30px;" id='myDiv'></div>
    <script language='javascript'>
    "use strict";
     var a = 4,
     i,
     div = document.createElement('div');

     //no leaks
     function noLeaks(b) {
         this.a = a;
         a = this.b;
     }
     var obj = new noLeaks(5);

     //leak pattern
    function makeLeaks(func) {
        div.badAttachToCOMObject = func; // the problem is here
    }
    makeLeaks(function(){}); //attach reference to JScript objcet/function?


    //no leaks
    function size() {
        return '30px'; //just a value
    }
    function noStyleLeaks() {
        div.style.width = size();
        /* DOM (COM in IE) element just get a value,
        not a reference, only if I put a object or
        function to the value is a problem? */
    }
    noStyleLeaks();

    //leaks or not
    function styleLeaks() {
        div.style.width = document.getElementById('myDiv').style.width;/* this
        is cycle reference, but maybe only between COM and COM objects?
        And do not make leaks? */
    }
    styleLeaks();
    </script>
</body>
</html>

Do I understand the main idea, or not? And one last question, Chrome has a task manager, that and windows task manager shows memory usage jump with ~1mb on every refresh when I use this from http://www.javascriptkit.com/javatutors/closuresleak/index.shtml:

function LeakMemory(){
    for(i = 0; i < 50000; i++){
        var parentDiv = 
        document.createElement("div");
    }
}

Chrome(21) make leaks? I think that is not normal. So I'm more and more confused in how JS garbage collection works.

user1091156
  • 239
  • 1
  • 2
  • 8
  • 3
    Give it time to GC the stuff, I am sure none of these leak memory in chrome. – Esailija Jul 19 '12 at 11:39
  • 1
    You also may want to take a look at this question: http://stackoverflow.com/questions/4324133/javascript-garbage-collection – Christoph Jul 19 '12 at 11:40
  • If you set "div" to null, you only will encounter exceptions but no memory leaks. Also, your noLeak "constructor" function is quite curious :-) – Bergi Jul 19 '12 at 11:56
  • 1
    Have you read [javascript, circular references and memory leaks](http://stackoverflow.com/q/1999840/1048572)? Is it a duplicate or do you ask for something different? – Bergi Jul 19 '12 at 11:58
  • @Bergi yes, I read this post before, just I read so many post, articles and now I'm really confused, so just want help. Ooo, and this "div = null" do not exist in the test scripts. My wrong with copying. – user1091156 Jul 19 '12 at 12:20
  • @Esailija Hah, I just click refresh every second. – user1091156 Jul 19 '12 at 12:21
  • @Christoph Thanks. I think I read something like that before, for that I think objects are sent to GC when do not have references, but the test in Chrome confused me... But now I know have to give a time to GC. – user1091156 Jul 19 '12 at 12:23
  • It really depends on the implementation of the GC. You can't speak of the GC as itself, as every browser has slight differences. – Christoph Jul 19 '12 at 12:29

0 Answers0