0

I'm using the featherlight library to show lightboxes. I'm using coffee and so at the beginning of my js file I have

myFeatherBox = void 0;

Later, when I want to use it I do stuff like this:

if (myFeatherBox != null) {
  myFeatherBox.close();
}
myFeatherBox = $.featherlight("<div>whatever</div>");

I test for null in case a featherbox is already opened, in which case I close it. To avoid leaving masses of these things in memory though, I'm essentially creating a new box in the old variable.

So I'm wondering whether that's just orphaning the old box in memory (and if so how to clean it) and whether this is the way to do things.

jcuenod
  • 55,835
  • 14
  • 65
  • 102
  • 1
    "whether that's just orphaning the old box in memory" - Yes, that's what it does. "how to clean it" - No need to worry, orphaned objects will be disposed of automatically by the garbage collection. – JimmyB Feb 26 '15 at 11:44
  • "whether this is the way to do things" - Yes, that's how it's done. In fact, there's no explicit way to remove an object from memory. Once it is no longer referenced, gc will take care of it. In gc'ed languages memory leaks only appear when objects are kept referenced although they won't be used anymore. This happens for example when you keep adding new objects to a growing array/list without ever removing them, even though you may only acutally use the last three of them. – JimmyB Feb 26 '15 at 12:17
  • BTW, no need to keep a reference to the current featherlight. You can call `$.featherlight.current()` at any point. Moreover, `$.featherlight.close()` will close the current featherlight if there is one. – Marc-André Lafortune Feb 26 '15 at 15:34

1 Answers1

0

If there are no more reference to the old feather box then it will be eligible for garbage collection (GC).

You can't force the garbage collection.

You can use tool like Chrome's Developer tools to see whats in memory.

Chris Charles
  • 4,406
  • 17
  • 31