Memory sensitive caches

M

Mark McKay

I have a large number of objects I load from disk and cache in a
HashMap. I'd like to know if the JVM is about to run out of memory,
and if so to then clear the eldest entries out of this cache. That
is, I'd like to be notified that an OutOfMemory error is about to be
thrown, and add some custom handling to try and free up space to
prevent it.

I've tried doing this with SoftReferences, but they're being cleared
long before I run out of memory.

Is there any way to implement a memory sensitive cache?

Mark McKay
 
M

Mike Schilling

Mark McKay said:
I have a large number of objects I load from disk and cache in a
HashMap. I'd like to know if the JVM is about to run out of memory,
and if so to then clear the eldest entries out of this cache. That
is, I'd like to be notified that an OutOfMemory error is about to be
thrown, and add some custom handling to try and free up space to
prevent it.

I've tried doing this with SoftReferences, but they're being cleared
long before I run out of memory.

Is there any way to implement a memory sensitive cache?

No; the information you need just isn't there. What I've done in similar
situations is build a mostly Soft-Reference-based cache in which the last N
objects referenced are also held in memory by hard references. It's not
ideal, because there is a risk of running out of memory if N is too large,
but with some tuning it can work quite well.
 
M

Mehmet Erdem

Mark said:
I have a large number of objects I load from disk and cache in a
HashMap. I'd like to know if the JVM is about to run out of memory,
and if so to then clear the eldest entries out of this cache. That
is, I'd like to be notified that an OutOfMemory error is about to be
thrown, and add some custom handling to try and free up space to
prevent it.

I do not think it is possible to get notified when an OutOfMemory error
is about to be thrown.
I've tried doing this with SoftReferences, but they're being cleared
long before I run out of memory.

Is there any way to implement a memory sensitive cache?

Mark McKay


I recently used a mixture of a LRUMap and a ReferenceMap (you can find
both in org.apache.commons.collections). The reference map is there to
hold as many objects as possible within the available memory (same as
your soft reference map) and the lru map contains a fixed (far smaller)
amount of the those who were last recently used. You only have to make
sure to update the lru map everytime you access an object within the
reference map. In this way your cache will only loose those entries
which have not been used recently.
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top