Benad's Web Site

If you are coding in C, C++ or Objective C, there are two things you are surely paranoid about: null pointers and memory leaks. Null pointers, because it crashes the whole process. Memory leaks, because objects you create in the heap by using malloc or new are not automatically deleted when the function (or the code block) that created it exits.

One design trick commonly used in Objective C to prevent memory leaks is simple: a function (or code block) is responsible to free the memory it allocated on the heap. If you are consistent using that design, then having memory leaks are less likely.

If you're lazy and don't want to write all this code to free memory, you can always use a garbage collector like Boehm's garbage collector for C. This could actually be faster than the code you would write to "figure out" what memory blocks to free in a set of structures that point to each other.

A problem remains though. Imagine you write a function that needs a total 100MB of temporary memory to work, in the form of thousands of little memory blocks. If you expect to call the function many times, then a lot of time will be wasted freeing memory you will allocate all over again the next time. In that case, you want to keep and reuse the memory you allocated just in case you're going to call the function again.

Well, in Java, even though you have a garbage collector silently running in a background thread, things are more complicated. Not only making a new object instance is more costly than a malloc in C, but if you create and throw away too many objects the garbage collector will unexpectedly slow down your code.

And so comes Javolution. This library lets you create objects from a "heap context", which is a Factory that recycles objects you don't use anymore. Because the context keeps a reference to all the objects it controls, Java's garbage collector will not collect those objects. That way, not only you will increase performance by not having to re-create those objects every time, but also the execution time of your function will be consistent. Or, another way of saying it, your function will have "real time" predictability.

Javolution also implements some typical classes from java.lang and java.util such as StringBuffer (Text and TextBuilder), ArrayList (FastList) and HashMap (FastMap) using contexts and the same design approach (reuse objects as much as possible), with spectacular performance improvements.

I wonder what would happen to the performance of real-time network-based Java applications such as Azureus, Freenet or i2p if they used a bit Javolution...

Published on January 18, 2006 at 15:24 EST

Older post: jEdit: It Does The Job

Newer post: Valgrind: Destroy the Corruption