How Garbage Collection can Increase Speed
By Bruce Eckel
In the October issue of WT, I spent most of my column explaining why Java couldn't possibly be as fast or faster than C++. Recently, while speaking at the Visual J++ Developer's Conference in Europe, I met Ian Ellison-Taylor, a developer from Microsoft's Java-development team, and Mike Toutonghi, who wrote significant parts of the Microsoft JVM. They patiently explained how Microsoft had implemented Java, and I began to see that much of my perspective stemmed from Sun's inefficient implementation, so there was no model to show how fast Java can be. All I had heard was Sun, saying "Java can be really great," and that wears thin after a while.
Part of my misunderstanding came from being cloistered with the C++ model for too long. C++ is focused on everything happening statically, at compile time, so that the runtime image of the program is very small and fast. C++ is also based heavily on the C model, primarily for backward compatibility, but sometimes simply because it worked a particular way in C so it was the easiest approach in C++. One of the most important cases is the way memory is managed in C and C++ this has to do with one of my more fundamental assertions about Java's slow speed: In Java, all objects must be created on the heap.
Allocating Objects
In C++, creating objects on the stack is fast because when you enter a particular scope the stack pointer is moved down once to allocate storage for all the stack-based objects created in that scope.