Solved

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

Posted on 2015-01-23
2
106 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Add bar graphs to Access queries using Unicode block characters. Graphs appear on every record in the color you want. Give life to numbers. Hopes this gives you ideas on visualizing your data in new ways ~ Create a calculated field in a query: …

623 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