title: How to start building a searchable garbage collector in Delphi (2009-2010)
body: <p>I'm looking for a way to control all business objects I create in my applications written in Delphi.</p>
  
  <p>As an article on Embarcadero's EDN (<a href="http://edn.embarcadero.com/article/28217" rel="nofollow noreferrer">http://edn.embarcadero.com/article/28217</a>) states, there are basically three ways to do this. I'm mostly interested in the last one, using interfaces. That way, when the business object is no longer being referenced anywhere in the application, it will be dispose of memory wise (I'll get back on this part later). </p>
  
  <p>When creating a new business object, it would be wise to ask that new object manager whether I already fetched it earlier in the program, thus avoid the need to refetch it from the database. I already have the business object in memory, so why not use that one? Thus I'll need the list of available objects in memory to be searchable (fast). </p>
  
  <p>The provided code there uses an "array of TObject" to store the collected objects, which doesn't make it very performant concerning searches through the list of objects once you get to a certain amount. I would have to change that to either TObjectList or some sort of binary searchable tree. What would be the best choice here? I already found some useful code (I think) at <a href="http://www.ibrtses.com/delphi/binarytree.html" rel="nofollow noreferrer">http://www.ibrtses.com/delphi/binarytree.html</a>. Didn't JCL have stuff on binary trees?</p>
  
  <p>How would I handle "business objects" and "lists of business objects" in that tree? Would a business object, being part of a list be referenced twice in the tree?</p>
  
  <p>Concerning the disposing of an object: I also want to set some sort of TTL (time to life) to that business object, forcing a refetch after an certain amount of time. 
  Should the reference counter fall to 0, I still want to keep the object there for a certain amount of time, should the program still want it within the TTL. That means I'll need sort sort of threaded monitor looping the object list (or tree) to watch for to-be-deleted objects.</p>
  
  <p>I also came across the Boehm Garbage Collector DLL (<a href="http://codecentral.embarcadero.com/Download.aspx?id=21646" rel="nofollow noreferrer">http://codecentral.embarcadero.com/Download.aspx?id=21646</a>).</p>
  
  <p>So in short, would it be wise to base my "object manager" on the source code provided in the EDN article? What kind of list would I want to store my objects in? How should I handle list of objects in my list? And should I still keep my object in memory for a while and have it dispose of by a threaded monitor?</p>
  
  <p>Am I correct in my reasoning? Any suggestions, ideas or remarks before I start coding? Maybe some new ideas to incorporate into my code?</p>
  
  <p>Btw, I'd be happy to share the result, for others to benefit, once some brilliant minds gave it a thought.</p>
  
  <p>Thnx.</p>