   Instant object removal, or periodic cleanup
  I'm creating a factory to create and store my object components. Each component has reference counting, so I could periodically check the reference count and cleanup them up that way. Or I could store them in a hash map by their pointer or an id and remove them instantly when needed. Which would be the preferred way? The cleanup method would be easier, but would require an update function to be called on the factory, the other way requires objects to call a remove function, but might be slower with a lot of hashmap lookups.   
  The more you can reuse components and reduce allocations during gameplay, the better.  Even more so if said components touch any external resources.  If you're not starved for memory, you can even wait until the end of a level or until the next loading screen to do all the cleanup.  Otherwise, you can wait until you've used X% of your memory budget and start cleaning up old components (although you don't want to do it all at once!).  Perhaps you can enforce a one-component-cleanup-per-frame rule to amortize the cleanup cost.  You basically have a bunch of options depending on your game's requirements.   ;Quote:Original post by M4573RI could periodically check the reference count and cleanup them up that way. Or I could store them in a hash map by their pointer or an id and remove them instantly when needed.What are you removing them from?I ususally check the reference count whenever it's decremented. e.g.void Object::DecrementReferenceCount(){  assert( m_RefCount > 0 )  --m_RefCount;  if( m_RefCount == 0 )    delete this;    //OR    m_System.ReturnToPool( this );};  I'd be removing from the active object list in my scene graph. Since I'll be iterating over it all the time, I wouldn't want old, unused object in it.Zipster brings up some good points though, perhaps I will have a pool of things to be deleted at the end of the level. However I'll still need to remove the object from my active list.   