471,350 Members | 1,930 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,350 software developers and data experts.

object references

Hi,

I have created a datalayer which connect with a SQL-Server and in the
datalayer I store the objects that are created in a static hashtable (for
performance reasons) so I don't have to recreated the objects when I need
them and I have then only one instance of that object in the memory.

My problem is the following, the created objects are stored in the hashtable
(when not yet present). After a certain time the hashtable becomes to big.
Is there a way to find out when an object isn't used anymore by the
application so I can delete it? In other words can I find out when an object
stored in the hashtable has no more references to it?

Thx in advance

Frederik
Dec 4 '05 #1
3 1353
Frederik Vanderhaegen <fr*******************@pandora.be> wrote:
I have created a datalayer which connect with a SQL-Server and in the
datalayer I store the objects that are created in a static hashtable (for
performance reasons) so I don't have to recreated the objects when I need
them and I have then only one instance of that object in the memory.

My problem is the following, the created objects are stored in the hashtable
(when not yet present). After a certain time the hashtable becomes to big.
Is there a way to find out when an object isn't used anymore by the
application so I can delete it? In other words can I find out when an object
stored in the hashtable has no more references to it?


You'd need to use WeakReferences - store the WeakReference in the table
instead of the real reference, and periodically check all the weak
references to see whether or not they're still alive, removing them if
not.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Dec 4 '05 #2
Frederik Vanderhaegen wrote:
Hi,

I have created a datalayer which connect with a SQL-Server and in the
datalayer I store the objects that are created in a static hashtable (for
performance reasons) so I don't have to recreated the objects when I need
them and I have then only one instance of that object in the memory.

My problem is the following, the created objects are stored in the hashtable
(when not yet present). After a certain time the hashtable becomes to big.
Is there a way to find out when an object isn't used anymore by the
application so I can delete it? In other words can I find out when an object
stored in the hashtable has no more references to it?


Look up the WeakReference class, it might be able to help you.

--
Lasse Vågsæther Karlsen
http://usinglvkblog.blogspot.com/
mailto:la***@vkarlsen.no
PGP KeyID: 0x2A42A1C2
Dec 4 '05 #3

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Frederik Vanderhaegen <fr*******************@pandora.be> wrote:
I have created a datalayer which connect with a SQL-Server and in the
datalayer I store the objects that are created in a static hashtable (for
performance reasons) so I don't have to recreated the objects when I need
them and I have then only one instance of that object in the memory.

My problem is the following, the created objects are stored in the
hashtable
(when not yet present). After a certain time the hashtable becomes to
big.
Is there a way to find out when an object isn't used anymore by the
application so I can delete it? In other words can I find out when an
object
stored in the hashtable has no more references to it?


You'd need to use WeakReferences - store the WeakReference in the table
instead of the real reference, and periodically check all the weak
references to see whether or not they're still alive, removing them if
not.


"Periodically" is the sticky part. I've been meaning to write a C#
replacement for Java's WeakHashMap, but haven't had the time to do
performance evaluation of the various choices: Check every N accesses, check
every N adds, check every N seconds, check only the current hash chain vs.
the whole map, etc.

It's a shame .NET doesn't have an analogue of Java's ReferenceQueues (i.e.
notifications when a weak reference no longer refers.)
Dec 4 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

6 posts views Thread by Chris S. | last post: by
44 posts views Thread by Steven T. Hatton | last post: by
3 posts views Thread by RobG | last post: by
5 posts views Thread by Michael Moreno | last post: by
16 posts views Thread by anonymous.user0 | last post: by
26 posts views Thread by yb | last post: by
275 posts views Thread by Astley Le Jasper | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.