… you would think. Maybe not, but many people seem to agree with that. One interesting fact is that, though, Java’s allocations are much faster than classic malloc allocations. This is due to the highly optimized nature of allocation in modern JVMs and the way they handle heap allocations and deallocations. Generational collectors is one of these techniques:
Generational collectors try to benefit from the empirically observed property that most allocated objects live for a very short time, while a small percentage of them live much longer. If an object survives one collection, chances are it will survive many collections. If the collector copies objects or compacts the heap, these long lived objects are copied over and over again. Generational techniques split the heap into several sub-heaps and segregates the objects in the sub-heaps according to their age. New objects are allocated in the first generation sub-heap. When the application runs out of memory, only the first generation sub-heap is scanned. The objects that survive one or more collections are moved to the second generation heap. If most objects are indeed short-lived, most of the first generation heap is freed and few objects need copying to the second generation heap. The older generation sub-heaps are scanned less frequently. Since smaller fragments of the heap are scanned and proportionally more storage space is recovered, the overall efficiency of the garbage collector improves.
Memory allocations are certainly just one aspect of application performance, and Java is still slow for performance-critical applications. Also don’t forget that programming languages like C and C++ still wins since most of the objects are allocated on the stack, hence free allocations.
Nevertheless, it is surprising to see that allocations in Java are even faster than malloc. Surprising as it is, calling the new operator in Java requires execution of about 10 instructions whereas a malloc call executes 60 to 100 instructions, hence making Java’s allocations 6 to 10 times faster! Make sure to check the article titled “Java theory and practice: Urban performance legends, revisited” by Brian Goetz if you are interested in details.
Finally, I’d like to share a few funny quotes this article reminds me about Java:
- If Java had true garbage collection, most programs would delete themselves upon execution. Robert Sewell
- Java: the elegant simplicity of C++ and the blazing speed of Smalltalk.
- Java is high performance. By high performance we mean adequate. By adequate we mean slow. Mr. Bunny
- Java is, in many ways,
C++--. Michael Feldman