ASP.Net Caching Question

  • Thread starter Thread starter Chris Ashley
  • Start date Start date
C

Chris Ashley

I'm currently storing a number of objects in application scope directly
and reference counting/disposing of them with the Session_OnEnd event.
I'm considering changing to a solution using the Application cache but
I'm a little unsure of its behaviour. If I add an object to the cache,
will ASP.Net automatically call the 'dispose' method on that object (if
available) when the specified timespan elapses?

Chris
 
Properly implemented objects will have their deconstructors (finalizers in
VB.NET) call Dispose when the object is about to be removed by the garbage
collector. This means that dispose will be called when on items expired from
the cache - but likely not right away - not until the GC actually passes.

This is actually true in all cases. The only reason you call Dispose is to
dispose of the object immediately - but it should eventually get disposed of
eventually (the only time I've seen it not happen is with a very bad system
that had references all over the places and the object was rooted
somewhere).

Karl
 
no. the cache just releases the reference (it has no way of knowing its
the only reference). the gc does the dispose, but it can be a long time
after the cache releases the object.

you probably should not cache objects that require dispose, as this
usually means they have references to unmanaged resources, which may
cause problems. for example its a really bad practice to cache sql
connections or datareaders.

-- bruce (sqlwork.com)
 
Back
Top