0

I have a case where I have a variable in my JavaScript that will be used to store the reference to several instances of a class. The class creates a div based window, kind of like a lightbox, and then when I call the class' removeWindow method, the div window elements are removed from the DOM. So a sequence might be like this:

var divWindow = null;
divWindow = new DivWindowClass(...);
/* do some stuff with the window */
divWindow.removeWindow();

Later on in the code I create a window again with different content, but I also want to be able to check if the divWindow variable is referencing anything.

Is it correct to just do:

divWindow.removeWindow();
divWindow = null;

if (divWindow == null) { /* etc. */ }

/* later in the code */
divWindow = new DivWindowClass(...); /* with different content than first one */

or does that cause memory issues with the JavaScript garbage collection? If I set divWindow to null, will that signal to the garbage collection to gather up my object and get rid of it, or is there something else I need to do to be tidy?

As always, thanks in advance!

8
  • 1
    You can also delete it if you want. Commented Aug 11, 2011 at 18:48
  • 1
    @pimvdb, Using delete on identifiers declared with the var statement (also function declarations, function argument names, etc) will have no effect (they are bound as non-configurable properties of its environment record), moreover this is disallowed on strict mode, delete identifier; is treated as a SyntaxError. See also: understanding delete Commented Aug 11, 2011 at 18:57
  • I did look into delete, but I thought delete couldn't be used on a variable declared with var? So I didn't think that was the way to go. If I used it, would it be delete divWindow; Would that set divWindow to null, or would I still have to do that so my later conditional works? Commented Aug 11, 2011 at 18:58
  • @CMS Beat me to it. This is what I read. Commented Aug 11, 2011 at 18:59
  • @CMS: Thanks. However if I do var a = 3; delete a, accessing a is not possible anymore. Commented Aug 11, 2011 at 18:59

2 Answers 2

1

Well it depends. If you have a function defined or expressed below the divWindow for example, that would mean divWindow is considered reachable by the garbage collector, at least for as long as that function exists. In that case, setting divWindow to null would actually cause the GC to clean it up on its next cycle. However, if that's not the case, "nulling" a variable won't help it anymore than normal. If you posted the entirety of your code, I could probably tell you more.

There is no explicit way to free up memory in javascript. The best you can do is to be aware of all references to your object, be aware of how reachable it is, and make sure the object is unreachable when you don't need it anymore. After you do that, ultimately, you have to learn to trust the garbage collector, which isn't so easy when you're coding something that you want to run in IE for example.

(ps: I also want to knock out a comment made above. The delete operator is for removing properties from an object. It doesn't "delete" variables, and it doesn't free up memory. In fact, javascript will throw if you do delete foo; in strict mode.)

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

2 Comments

Your comment: "make sure the object is unreachable when you don't need it anymore", is my setting divWindow to null the way to tell that to the GC? Or a safe way?
Accepting this answer because I appreciated the extra explanation. Thanks!
0

Doing the first solution is fine. This will signal the garbage collector, but it is infinitesimal to how much the garbage collector can take.

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.