2

My node/javascript program receives lots of messages. Each time a message is received I create a new object, passing it message content. Within the new objects constructor, the object does a bunch of stuff, including some mongo operations with callbacks.

When the operations that are performed within the object constructor are complete, the object is no longer needed or wanted.

After some experiments involving complexity (i.e. storage of the object in an array element or as a property of a 'master' object and then deleting it, I tried the simple approach.

var x = new DisposableObject(message);
delete x;

This seems to work fine. There may be many instances of DisposableObject in existence at any time. DisposableObject is created and does everything expected of it in tests. I am assuming that when I delete 'x' that this does not affect the object itself, just the reference to it that is contained in 'x'. The objects callbacks occur and all is good.

So four questions:

  1. Will this simple mechanism allow the garbage collector (node/V8) to get rid of the object?
  2. Is there some awful trap door awaiting my code because I am doing this?
  3. Is there any issue with multiple instances of DisposableObject floating around waiting for callbacks when there is no active reference left to them in my program?
  4. Is there a better way to create the objects, then make sure they can be garbage collected after all their operations are completed?
RoyHB
  • 1,715
  • 1
  • 22
  • 38
  • delete x; doesn't do what you describe, unless x is global and even then it doesn't do exactly what you're thinking... – dandavis Dec 22 '14 at 06:14
  • 1
    private vars in callbacks or any function will be cleaned up when the function is done if nothing else needs the data within. – dandavis Dec 22 '14 at 06:20
  • So I think that you're saying that I don't need to do anything to allow the object to be gc's. It is in fact created within a callback – RoyHB Dec 22 '14 at 06:22

1 Answers1

3

Javascript uses a nongenerational mark-and-sweep garbage collector, in javascript objects are automatically garbage collected when they are no longer needed , so you need not worry about garbage collection.

but you have to keep these points in mind (from this answer):

  1. what you are trying with delete does not really delete an object, delete is only effective on an object's properties. It has no effect on variable or function names., Use delete statements whenever you create an object using a new statement, pair it with a delete statement. This ensures that all of the memory associated with the object, including its property name, is available for garbage collection. The delete statement is discussed more in “Freeing Objects.”

  2. Use the var keyword. Any variable created without the var keyword is created at the global scope and is never eligible for garbage collection, presenting the opportunity for a memory leak.

  3. In case of global variables, Global variables are never disposed of by the GC in the sense that a global variable will always exist. Setting it to null will allow the memory that it references to be collected. the memory used by the object will be eligible for collection. but The variable still exists though, and it simply references null(some more here)

Community
  • 1
  • 1
Naeem Shaikh
  • 15,331
  • 6
  • 50
  • 88