Intelligent memory management in Java

Why should you worry about memory usage in Java? Because as much as memory is cheap, conserving it is still important. It would be stupid if Notepad (or KWrite) ate through 512Mb of memory before you’d opened a document… On the server side, if each of your users required 64Mb of session memory, you’d quickly run into performance issues when dealing with thousands of users.

So how can you conserve memory in Java. The first (and most obvious) way, is through the reference API: java.lang.ref. Using a SoftReference means that the object “behind” the reference can be garbage collected when memory is running low. Combined with what can be though of a lazy-loading, objects can be effectively cached in memory.

EoD SQL makes use of WeakReference’s to maintain == (identity) equality. A WeakReference will be valid until the object behind it is garbage collected. The garbage collection can take place as soon as no hard references to the object exist. This is important in EoD SQL, since if the calling code (application or web-app) doesn’t hold a reference to the object, how can it test for any kind of equality? To add to this point, EoD SQL may be dealing with very large volumes of data (since a database ResultSet may be millions of rows long), and holding hard-references may lead to OutOfMemory errors. Holding SoftReferences is also a bad idea, since working through a large ResultSet may lead to performance issues when many threads are working with similar sized ResultSets, and all of them or trying to keep the data in-memory.

For small objects, it is actually a good idea not to cache them in memory (with or without a hard-reference). Wherever possible, cheap objects should be short-lived, the eden-space garbage collector runs frequently and very quickly. Watch where you reference objects from! Lazy Loading is often a good idea, and can be strongly enhanced with Lazy Discarding (using a SoftReference).

For more information on how the garbage collector in Java works (and a bit of info on ClassLoading), take a look at the short presentation I made here.


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s

%d bloggers like this: