Using SoftReferences for caching

J

Jesper Nordenberg

I'm writing an application where I have a memory cache of objects
stored on disk. There are no limitations on the object sizes, one
object can be as large as the heap. A long value is used to identify
an object, so there can be a huge amount of objects.

I want to use a fixed amount of the heap or all the available heap for
the cache. I can't cache a fixed number of objects since I don't know
how how much memory an object occupies, and thus the program can run
out of memory. So, using SoftReferences seems like a good solution.
One problem is that the HashMap used in the cache can become very big
and needs to be cleared of SoftReferences that has been nullified.

If someone have some implementation or design suggestion that would be
great. Maybe there is some open source project that implement a
similar cache?

/Jesper Nordenberg
 
C

Conor White

You need to associate each SoftReference with a ReferenceQueue object.
When a SoftReference is Garbage collected it is placed in the
ReferenceQueue. This gives you a chance to do a cleanup before the
object is finally lost.
 
C

Carl Howells

Conor said:
You need to associate each SoftReference with a ReferenceQueue object.
When a SoftReference is Garbage collected it is placed in the
ReferenceQueue. This gives you a chance to do a cleanup before the
object is finally lost.

Be careful which object you're talking about... When a SoftReference is
placed in a ReferenceQueue, the object the SoftReference referred to has
already been collected. There is no way to get the object back at that
point. All you can get out of the ReferenceQueue is the SoftReference.

This does still solve the OP's problem... But be careful how you phrase
things.
 
J

Jesper Nordenberg

You need to associate each SoftReference with a ReferenceQueue object.
When a SoftReference is Garbage collected it is placed in the
ReferenceQueue. This gives you a chance to do a cleanup before the
object is finally lost.

I tried this. Two problems I encountered:

- You need another (reverse) HashMap to look up the entry of the
SoftReference that was placed in the ReferenceQueue.

- When testing with multiple threads I sometimes ran out of memory. It
seemed there was some nullified SoftReferences in the map that haven't
been placed in the ReferenceQueue. Maybe it was an error in my
program.

My current solution is to regurarly traverse the map and remove
nullified SoftReferences.

/Jesper Nordenberg
 
A

Adam Jenkins

Jesper said:
I tried this. Two problems I encountered:

- You need another (reverse) HashMap to look up the entry of the
SoftReference that was placed in the ReferenceQueue.

Typically you would just use your own subclass of SoftReference, with
some extra fields containing whatever information you need to clean up
after it. No need for a reverse map.

Adam
 
C

Conor White

Look at the code for the java.util.WeakHashMap class. This does what
you want except it uses weak references instead of soft references.
 

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

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top