1

I am building something for mobile and would like somehow to clear, null objects, variables to release a bit of memory. Here I have two quick examples, both are anonymous functions as I believe but what way is better or more valid approach? Sorry if I get it all wrong. To me both seem to do the same thing although I like more the first one as objects wont be created until I need it. The second version would immediately execute the code for creating variables, objects etc. but not doing the main build function until I need it.

I am just trying to figure out what way is more common. I know that beginners like me mostly misunderstand the use of anonymous functions.

V1

var app = function() {

        //create variables, objects
        var a = 'Data 1';
        var b = 'Data 2';

        //do some things    
        console.log(a + ', ' + b);

        //do a cleanup
        app.cleanup = function() {

            a = null;
            b = null;
            console.log(a, b);

        }
    }
    setTimeout(app, 200);

V2

var app = {};

    (function(){

        //create variables, objects
        var a = 'Data 1';
        var b = 'Data 2';

        app.build = function(){                 

            //do some things
            console.log(a + ', ' + b);          

        }

        //do a cleanup
        app.cleanup = function(){

            a = null;
            b = null;
            console.log(a, b);

        }   

        setTimeout(app.build,200);

    })();

Later in html or event

<input type="button" onclick="app.cleanup()" value="clean" />
Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
devjs11
  • 1,898
  • 7
  • 43
  • 73
  • 1
    You shouldn't be worrying about freeing up resources. JavaScript has a garbage collector which will pick up variables which fall out of scope and destroy them. Delete a reference to an object when you don't need it (`delete obj.yourReference`, / `reference = null` / `reference = undefined`) and let the garbage collector do the rest. – Matt Nov 29 '11 at 11:50
  • but to delete a reference or null it would you go with V1 or V2 of mine? I mean I still need to do that somewhere. – devjs11 Nov 29 '11 at 11:54
  • Or simplifying what @Matt said: if the variable becomes unavailable (no other method is able to access it in any way), it is marked for garbage collection. Basically, if a variable was created in a function (local variable, as in your case), after the function ends, nobody would be able to access it, so it will be automatically cleaned up. – bezmax Nov 29 '11 at 11:55
  • You should be aware that effectively, you are not cleaning anything. Strings in JavaScript are immutable. The literals "Data 1" and "Data 2" are already part of your program, so a and b just point to those already existing strings. Setting them to null just removes one reference to those strings. – Erich Kitzmueller Nov 29 '11 at 11:56
  • @Alex: As it stands, #1 does not need any cleanup. Remove your `app.cleanup()` definition, and `a` and `b` will be automatically reclaimed by the garbage collector, as nothing is holding a reference to them once the function has finished executing. – Matt Nov 29 '11 at 12:03
  • @Matt And what will happen to global variable "app"? If the function inside is large enough will it affect somehow? – devjs11 Nov 29 '11 at 12:06
  • @Alex: #2 doesn't make sense. Because you're defining them in the closure, they are always available until you remove the references to `app`. `cleanup` will remove the strings, but you'll still have the variable definitions and the function declaration loitering around. Plus, what happens if someone wants the variables again? They're gone! Once you've finished using a resource, remove the reference to it and the garbage collector will handle it. – Matt Nov 29 '11 at 12:06
  • @Alex: In #1, because `app` is in the global scope, `window` holds a reference to it. If you do `delete window.app` or `app = null` (providing nothing else is holding a reference to it; e.g `var x = window.app`) the function will be removed. – Matt Nov 29 '11 at 12:07
  • @Matt Thank you!!! If you would like to summarize what been said, It will be my pleasure to accept your answer. – devjs11 Nov 29 '11 at 12:12
  • @Matt Just a quick question. What about normal functions like "function someName(){}" Calling those someName(); variables of it are not inside the global scope so I should not worry about it? – devjs11 Nov 29 '11 at 13:03
  • @Alex: I'm not too sure what you mean? If you're talking about variables internal to `someName`, then as long as you're not assigning the variables to anything that persists after the function stops executing, or returning a reference to one of the variables, they will all be reclaimed. If you see http://jsfiddle.net/vQtZN/, you can see neither `obj2` or `obj1` are being reclaimed, because a reference to them still exists (because `obj2` is returned from the function). `obj3` however, is unreferenced and so will be reclaimed. – Matt Nov 29 '11 at 13:28

1 Answers1

2

You shouldn't be worrying about freeing up resources. JavaScript has a garbage collector which will pick up variables which fall out of scope and destroy them. Delete a reference to an object when you don't need it using delete obj.yourReference, reference = null or something similar, and let the garbage collector do the rest.

You'll find that #1 will automatically reclaim the a and b variables itself automatically, if you remove your app.cleanup() definition. Unless you do that, a and b are both enclosed in the closure created by the cleanup function you're leaving behind, so you're stopping the garbage collector from doing it's thing.

To get rid of the whole app in #1, you'll have to do delete window.app or app = null as window holds a reference to it.

Matt
  • 74,352
  • 26
  • 153
  • 180