Leak-free context - requesting feedback

From: Andrus Adamchik (andru..bjectstyle.org)
Date: Tue Nov 14 2006 - 10:02:53 EST

  • Next message: Tore Halset: "Re: Leak-free context - requesting feedback"

    As we all know, DataContext doesn't clean up unused registered
    objects on an assumption that the entire DataContext has a finite
    lifespan and will be garbage-collected eventually.

    In my session-less web application I am using an app-scoped read-only
    DC shared by all requests. Sure enough it leaks memory, given a big
    enough database. In the past we solved this problem by periodically
    replacing a shared instance of DC with a new one. Since this approach
    intrerfered with the caching mechanism and generally seemed dirty,
    the other day I implemented a self-cleaning ObjectStore based on
    commons-collections ReferenceMap:

    class LeakFreeObjectStore extends ObjectStore {

       public LeakFreeObjectStore(DataRowStore dataRowCache) {
           super(dataRowCache);
           objectMap = new ReferenceMap(AbstractReferenceMap.HARD,
    AbstractReferenceMap.WEAK);
       }
    }

    Note that I couldn't use LRUMap, as we can't unregister objects that
    are referenced by other application objects, so instead I used weak
    references to cleanup otherwise unreferenced instances. Been watching
    this in production for a day and it works perfectly.

    Now the question is whether we want this behavior as a default (or as
    an option) for the DataContext? In other words should I move this fix
    to Cayenne? (I will have to modify it to create hard references to
    the dirty objects as we can't deallocate those even if they are not
    referenced in the app).

    I'd say yes, but I was wondering if premature garbage collection of
    registered objects is bad for any reason? Thoughts?

    Andrus



    This archive was generated by hypermail 2.0.0 : Tue Nov 14 2006 - 10:03:19 EST