WeakHashMap gives up to easily

C

Chris

I'm trying to build a memory-sensitive cache using WeakHashMap. The
trouble that I'm having is that the system dumps the references (that
is, gc()'s them) long before it needs to. I'm getting way too many cache
misses. When I replace WeakHashMap with a regular HashMap, things work
great. Of course, HashMap won't work in production because we'll run out
of memory. How do I get WeakHashMap to stand its ground and not dump
references before it really needs to?

Or alternatively, is there a decent cache class out there already so I
don't have to write one?
 
E

EJP

Chris said:
I'm trying to build a memory-sensitive cache using WeakHashMap. The
trouble that I'm having is that the system dumps the references (that
is, gc()'s them) long before it needs to. I'm getting way too many cache
misses. When I replace WeakHashMap with a regular HashMap, things work
great. Of course, HashMap won't work in production because we'll run out
of memory. How do I get WeakHashMap to stand its ground and not dump
references before it really needs to?

You can strengthen it by holding a SoftReference to the key in the value
object.
 
Z

Zaph0d

I'm trying to build a memory-sensitive cache using WeakHashMap. The
trouble that I'm having is that the system dumps the references (that
is, gc()'s them) long before it needs to.

You misunderstand weak and soft references.
Object that is referenced by regular references - doesn't get GCed.
Object that is referenced by soft references and not any regular
references - get GCed only on low memory conditions (full heap, etc.
read jvm docs).
Object that is referenced by weak references and not any soft or
regular references - get GCed in the next GC run. <-- Same as object
that is not referenced at all.

Weak references should be used for secondary maps.
Let's say you have a factory creating unique keys and another method
that gets a key and uses it to retrieve a previously created object.
Now, as long as someone has the key (regular/soft reference) he can use
the get function to get the object - we need to save it. However, when
the key is lost (GCed), the object cannot be accessed any more and it's
safe to GC it. That's what WeakHashMap is for - if the key is lost the
value get GCed.

For caching the situation is different. You may have a factory creating
repeatable keys by some parameter. That means that a key can be
recreated after it's lost. That's where you use SoftHashMap (I think it
exists, didn't look it up. if not, it's a 1 minute class). the get
method will check the map - if the object still exists, it returns it
(thus elevating the object to be referenced by regular reference until
the client throws it out). if it doesn't exist, the get function call a
create function that knows how to parse the key into a full blown
object.
Your code may vary, but that's the idea behind weak/soft references.

Please notice that if you need a really good cache managment, you'll
probably need to build it yourself and not rely on java GC.
 
H

Hendrik Maryns

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Chris schreef:
I'm trying to build a memory-sensitive cache using WeakHashMap. The
trouble that I'm having is that the system dumps the references (that
is, gc()'s them) long before it needs to. I'm getting way too many cache
misses. When I replace WeakHashMap with a regular HashMap, things work
great. Of course, HashMap won't work in production because we'll run out
of memory. How do I get WeakHashMap to stand its ground and not dump
references before it really needs to?

Or alternatively, is there a decent cache class out there already so I
don't have to write one?

I think Jakarta Commons Collections’ ReferenceMap is ideally fit for
memory-sensitive caches.

H.
- --
Hendrik Maryns
http://tcl.sfs.uni-tuebingen.de/~hendrik/
==================
http://aouw.org
Ask smart questions, get good answers:
http://www.catb.org/~esr/faqs/smart-questions.html
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.2 (GNU/Linux)

iD8DBQFE/pQYe+7xMGD3itQRAlcdAJ0WAO2Oio2ur09AtXV4xdlo+cHWmQCfaU4+
hLeHmDG9TvDmQ6Yl5jy5hAo=
=LwdO
-----END PGP SIGNATURE-----
 

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

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,186
Members
46,744
Latest member
CortneyMcK

Latest Threads

Top