Soft and weak references

5 views
Skip to first unread message

Dmitry Shaporenkov

unread,
Sep 4, 2003, 4:10:49 AM9/4/03
to
Hi all,

it seems for me that in Java world there is much freedom
in controlling the garbage collector than in .NET: JDK
includes SoftReference and WeakReference classes,
while .NET supports only WeakReferences. Is there any way
to simulate soft references in .NET? Or why they are
missed?

I'm not a guru in .NET garbage collector, and I have a
problem: in my Java application there is a cache based on
soft references, and all works fine. When I ported the
application to the .NET, the cache becomes using weak
references, and it's behavior is now far from good: weak
references die too often, so the application needs to
rebuild cached object which is time-consuming procedure.

Thanks in advance,
Dmitry

Jay B. Harlow [MVP - Outlook]

unread,
Sep 4, 2003, 10:48:08 AM9/4/03
to
Dmitry,
For those of use not fluent in Java, can you explain (or give link) on what
a SoftReference is & how you would use it in your Cache?

Then I or someone else may be able to offer suggestions on how to better
approximate them in .NET.

At the very least it does sound like something I would find useful.

Hope this helps
Jay


"Dmitry Shaporenkov" <dsha...@yahoo.com> wrote in message
news:11f701c372bc$0d2480a0$a001...@phx.gbl...

Jay B. Harlow [MVP - Outlook]

unread,
Sep 4, 2003, 10:59:22 AM9/4/03
to
Dmitry,
Does the "Caching Application Block for .NET" give you closer to what you
want?

http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpag/html/CachingBlock.asp

Granted its not SoftReferences per se.

Hope this helps
Jay

"Dmitry Shaporenkov" <dsha...@yahoo.com> wrote in message
news:11f701c372bc$0d2480a0$a001...@phx.gbl...

no potted meat@hotmail.com David Browne

unread,
Sep 4, 2003, 11:10:30 AM9/4/03
to

"Dmitry Shaporenkov" <dsha...@yahoo.com> wrote in message
news:11f701c372bc$0d2480a0$a001...@phx.gbl...

no potted meat@hotmail.com David Browne

unread,
Sep 4, 2003, 11:33:12 AM9/4/03
to

"Dmitry Shaporenkov" <dsha...@yahoo.com> wrote in message
news:11f701c372bc$0d2480a0$a001...@phx.gbl...

The closest thing I can think to SoftReferences would be long WeakReferences
to finalizable objects.

In .NET you have 2 kinds of WeakReference, and resurrection to play with.

For instance if you use long WeakReferences -- new WeakReference(o,true) --
and o has a Finalizer, then you get to decide, in O's finalizer whether it's
time to die. If it's not time to die, then just
GC.ReRegisterForFinalize(this).

First off, merely using long WeakReferences to finalizable objects for your
cache will make them behave more like SoftReferences. A finalizable object
alway survives at least one GC, and thus gets promoted to Generation 1.
Generation 1 is collected less frequently than Generation 0, and only when
more memory is needed. Generation 2 is collected even less frequently than
Generation 1. So if you re-register for finalize just once then you're
guaranteed to survive into Generation 2 and will probably be around for a
good long while.

David


Reply all
Reply to author
Forward
0 new messages