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" />
11
  • 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. Commented Nov 29, 2011 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. Commented Nov 29, 2011 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. Commented Nov 29, 2011 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. Commented Nov 29, 2011 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. Commented Nov 29, 2011 at 12:03

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.