Caching using weak references

Some time ago I wrote a Java class that caches generated images so I don’t need to re-render them all the time (rendering SVG graphics takes some time…). There’s only one problem: As more and more different images are getting cached, the maximum heap space of the JVM will eventually get exhausted. Increasing this limit is not an option. This can be “fixed” by using a queue for the images: If it is full, the first image cached will get discarded so the memory usage will be limited. This can be improved by implementing a least recently used (LRU) caching strategy, which is exactly what I did.

This has the drawback that even if there is plenty of memory remaining, cached images are deleted if the queue is full and a new image is about to be added. Today, I learned about the java.lang.ref package and especially about it’s SoftReference class. Objects referenced by SoftReferences will be deleted by the garbage collector if the JVM runs out of memory, which should be the ideal way to implement a cache…

You can learn more about it at Wikipedia and IBM Developer Works.

Leave a Reply

Your email address will not be published. Required fields are marked *