Solved

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

Posted on 2015-01-23
2
89 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
Comment Utility
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
Comment Utility
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

This document covers how to connect to SQL Server and browse its contents.  It is meant for those new to Visual Studio and/or working with Microsoft SQL Server.  It is not a guide to building SQL Server database connections in your code.  This is mo…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

743 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