ASP.Net Caching Question

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
 
K

Karl Seguin

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
 
B

bruce barker

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)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top