object references

  • Thread starter Frederik Vanderhaegen
  • Start date
F

Frederik Vanderhaegen

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
 
J

Jon Skeet [C# MVP]

Frederik Vanderhaegen said:
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.
 
?

=?ISO-8859-1?Q?Lasse_V=E5gs=E6ther_Karlsen?=

Frederik said:
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.
 
M

Mike Schilling

Jon Skeet said:
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.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top