title: Node.js garbage collection and synchronized objects
body: <p>This is a little bit tricky to explain, but I'll give it a try:</p>
  
  <p>In a node.js server application I would like to deal with data objects that can be used in more than one place at once. The main problem is, that these objects are only referred to by an object id and are loaded from the database.</p>
  
  <p>However, as soon as an object is already loaded into one scope, it should not be loaded a second time when requested, but instead the same object should be returned.</p>
  
  <p>This leads me to the question of garbage collection: As soon as an object is no longer needed in any scope, it should be released completely to prevent having the whole database in the server's memory all the time. But here starts the problem: </p>
  
  <p>There are two ways I can think of to create such a scenario: Either use a global object reference (which prevents any object from being collected) or, really duplicate these objects but synchronize them in a way that each time a property in one scope gets changed, inform the other instances about that change. </p>
  
  <p>Again, therefore each instance would have to register an event handler, which in turn is pointing back to that instance thus preventing it from being collected again.</p>
  
  <p>Did anyone come up with a solution for such a scenario I just didn't realize? Or is there any misconception in my understanding of the garbage collector?</p>
  
  <p>What I want to avoid is manual reference counting for every object in the memory. Everytime when an object is being removed from any collection, I would have to adapt the reference count manually (there is even no destructor or "reference decreased" event in js)</p>