This is my blog in English, about the stuff I do for a living, and about the stuff I read about the stuff I do for a living - when I am not working.

Thursday, March 19, 2009

I learned a new thing today...

I finally understood what weak references are for:

http://weblogs.java.net/blog/enicholas/archive/2006/05/understanding_w.html

Simply put, the referenced object (by one of the reference classes) can be claimed by the garbage collector at any time, and you wouldn`t have that object anymore. Why the hell would I want to use that, then?

The answer is simply: caching.

By doing some hack around the class SoftReference, you could have a caching mechanism that would delete objects from the cache when the Java VM needs more memory. Instead of crashing, you would lose some objects from the cache, which would have to be obtained later, but no big deal - that`s just a small performance penalty.

I would like to have that knowledge 5 years ago. It would have been useful.

No comments:

Post a Comment