?
Solved

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

Posted on 2015-01-23
2
Medium Priority
?
111 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 2000 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

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
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:…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

719 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