Search code examples
javac++performanceheap-memoryallocation

Why is C++ heap allocation so slow compared to Java's heap allocation?


I ran small tests in Java and C++, creating tons of very small objects (no class members, nothing in constructors) and Java is clearly faster (I mean C++ seems to be really slow). I bet this has to do with the JVM but in which way ?

Edit:

The classes I used were like this (as I said no class members, nothing done in constructors) in C++ :

class foo{
    public:
        foo(){}
        ~foo(){}
}

And in Java :

public class Foo{
    public Foo(){}
}

The small tests I made were only about allocating tons of objects in a loop (arround 1000000000 in a row). I used GCC 4.7.2 and Java 1.7 through OpenJDK implementation, both on the same machine using Linux.

I bet that indeed it has to do with memory pool allocation, which indicates that the JVM owns uneeded memory.

I'm still confused because I thought JVM would actually be slower, counting pointers references and allocating memory.


Solution

  • Allocation of many small objects on the heap is one situation where Java's runtime is often better optimized out-of-the-box than C++ (in most mainstream implementations). Every time you allocate a heap object in C++ using new, the implementation will usually make a system call to the OS (at least on most mainstream platforms like Linux and Windows). In Java, it is usually allocating from a memory pool provided by the JVM, which is specially designed and optimized for allocating Java objects on the heap.

    C++ will likely be faster in most situations if you use a special memory pool allocator. (Also, C++ gives you the option to allocate objects on the stack, which, of course, is much faster than any of this.)

    In general, C++ gives you much more fine-grained control over how your program allocates and manages memory. Whereas JVMs are constrained by the actual Java language spec which requires heap-allocated objects and garbage collection. But if you're writing an application in C++ that needs to allocate many small objects on the heap, you might want to consider using a memory pool allocator.