Skip to main content

Disk backed object cache or heap with swap

1 reply [Last post]
Joined: 2005-03-18

I have a Java application that maintains a cache of objects that are a little expensive to recreate. The numbers of objects needs to be increased. I was wondering if there is any benefit to using a disk backed cache, like ehcache, instead of just allowing the Java heap to begin to be swapped in and out of memory. Are there any pointers or information that can provide any insights before prototyping a solution and trying it?

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Joined: 2006-02-15

With swapping, you're not in control, and you are quite likely to end up with a working set larger than your available RAM so you'll start thrashing. In addition if you read/write your objects to disk yourself, your OS is highly likely to use memory that the JVM isn't using, to cache those files.

You might want to read up on how ehcache structures its cache and resolves cache requests... I might be recalling this inaccurately, but I think that it is a mapping from a key to an array of field values. Getting an object from the cache returns a newly created object with all the fields set to the values from the array.

If you chose to serialize objects to disk yourself, make sure you read the javadocs for (in addition to Serializable).

A LinkedHashMap can be very useful for an LRU cache.

Are you able to optimize the objects you're caching? you might be able to make them take up a lot less space, thereby increasing how many you can store in memory before needing to consider using a disk. Or you might be able to optimize them for fast serialization/deserialization.