Solved

.NET managing a cache of self maintaining objects - disposal issue?

Posted on 2015-01-23
2
91 Views
Last Modified: 2015-01-23
In my asp .net environment I have a collectionbase of a proprietary class, each of which represents a set of data for a period. e.g Quarter-To-Date, Yesterday, Today etc.
when the site is started, each class is added to an application scope collectionbase which handles queries by date range such that if a user requests dates which match one of these often requested date ranges the set of data concerns is pulled from the collectionbase in the form of the proprietary class else it is prepared on an ad hoc basis.
Each class contains a system.timer.timers object in order to allow it to check for changes in the underlying data every so often.
The collection base itself also runs a system.timer.timers object which ensures that at midnight the date ranges themselves are reset as 'Yesterday','Today' etc become different dates, so the collectionbase is dropped using me.clear and the proprietary classes are recreated and added back in.

My problem is that the collectionbase members despite having been cleared carry on living, with the timers inside triggering causing the class to carry on requerying the database meaning as each day passes I have more and more objects which I had assumed because they cannot be referenced because I have cleared the collectionbase would have been disposed of.

Is the use of timers a cause of the objects inability to die or is there something else I am missing about the way in which collectionbases and their members are disposed of when me.clear is used?

Thank you for your input.
0
Comment
Question by:dgloveruk
2 Comments
 
LVL 40

Accepted Solution

by:
Jacques Bourgeois (James Burger) earned 500 total points
ID: 40566663
An object does not "die" right away when you get rid of it. It hangs out in the Garbage Collector (GC) that disposes of it when it needs resources.

Most of the time, it causes no problem, because most objects are idle unless you use them.

But in other cases, something might still being active under the hood. This is the case with some types of connections and opened files for instance. This is also the case with timers.

The easiest way to make sure that these do not cause problems is to create a method that you call before you get rid of the object. That is what you do when you call Close on a database connection or a file.

You should to the same with classes that use timers. Have some Close method that stops the timers and call it before you call Clear.

If you a willing to go a little further however, do it the framework way, by implementing a Dispose method. The Dispose method plays the same role as Close, and should usually be called by the programmer who uses the class, but if done properly, it will also be called by the Garbage Collector when it reclaims the memory used by the object. This is a safegard in case the programmer did not call Dispose.

Note that many classes have both a Close and a Dispose method. Typically they do the same thing. The Close is there for programmers who are not aware of the Dispose mechanism but retain the habit of calling Close from their previous programming experience.
0
 

Author Closing Comment

by:dgloveruk
ID: 40566908
Thank you so much Jacques for your well considered and clear answer.
I went back to my solution and put a 'for each loop' in prior to the clearing which went through each object and disposed of it which resolved the problem.
Until this solution I had not had cause to be aware of how disposals may affect software but now thanks to your reply I will be ready in the future.
Kind Regards,
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This Micro Tutorial will give you a basic overview how to record your screen with Microsoft Expression Encoder. This program is still free and open for the public to download. This will be demonstrated using Microsoft Expression Encoder 4.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now