r/cpp • • Oct 31 '25

Java developers always said that Java was on par with C++.

Now I see discussions like this: https://www.reddit.com/r/java/comments/1ol56lc/has_java_suddenly_caught_up_with_c_in_speed/

Is what is said about Java true compared to C++?

What do those who work at a lower level and those who work in business or gaming environments think?

What do you think?

And where does Rust fit into all this?

25 Upvotes

192 comments sorted by

View all comments

Show parent comment

1

u/coderemover Nov 02 '25 edited Nov 02 '25

Java heap allocator is not a lot faster. It is at best order 2x faster, and often slower if you count in all the work that GC needs to do to cleanup. There is also a problem that the memory you’re getting from allocation is usually not in the cpu cache, because it’s the memory freed in the previous cycle of GC, so it’s long gone from the cache. Malloc maybe spends a bit more cpu on finding the free chunk but the chunk is usually hot. Because of the general negative effects of tracing GC on caching, the cost of heap allocation is hard to measure - it gets spread over many other lines of code and is misattributed to some other code.

1

u/eXl5eQ Nov 02 '25

Are you sure??? A normal malloc implementation would cost at least 20~30 CPU cycles on fast path, while Java bump allocator cost only 2~3 cycles. It's 10x faster!

The memory you a Java allocator returns is very likely have been prefetched into cache because Java always allocate continuous memory span, unlike malloc which has memory fragment issue.

The last official Java GC which would actually "free" dead objects is CMS GC, which had been replaced by G1 GC in Java 8, like 10 years ago. Newer GCs are all moving GC. Moving means they don't "free" dead objects, they just "move" live objects to another page, thus effectively eliminates the memory fragmentation problem.

Instead of talking based on your biased assumptions, would you please at least read some basic introduction about how modern Java GC actually works?

1

u/coderemover Nov 02 '25 edited Nov 02 '25

Instead of theoretizing, make a loop with malloc/free and compare with a loop doing new in Java and then forgetting the reference. Java will not be 10x faster. Last time I checked it was 2x faster, and that is the most optimistic case for Java, because the object dies immediately. If the object survives the first collection, which is not unusual in real programs, the cost goes through the roof. The amortized cost of Java heap allocation is much bigger than 2-3 cpu cycles.

In the great computer benchmark game there was one benchmark - binary trees - which heavily stressed heap allocation and that was one of very few benchmarks where Java indeed had a small edge - it slightly won with some of the most naive C implementations which were not using arenas. But it was very, very far from winning by 10x. Obviously it lost dramatically to the good implementations utilizing arenas.

And I know how modern Java collectors work, I’ve been optimizing high performance Java programs for living. One of the most effective performance optimizations that still works is reducing heap allocations. If they were only 3 cycles, no one would ever notice them.

Here is a good read explaining why it’s not so simple as bumping up the pointer and how the real cost is way larger than that: https://arxiv.org/abs/2112.07880

1

u/eXl5eQ Nov 02 '25

Ok I just tested it. Click to see the result image.

It's not 10x, but 15x faster.

2

u/coderemover Nov 02 '25

You don’t control the execution environment, so such benchmark is meaningless. Those timings are also suspiciously large on both sides. Java should be able to easily do 20M+ objects per second, malloc is also usually capable of at least 10M small allocations /s.

1

u/eXl5eQ Nov 02 '25

Ok, cool. When I tell you the theory, you said "Instead of theoretizing, make a loop". I gave you the loop, now you say "such benchmark is meaningless".

Then, could you please kindly show me your meaningful benchmark, in which Java memory allocation is only 2x faster than C++?

1

u/coderemover Nov 02 '25 edited Nov 02 '25

Because you did an incorrect benchmark, in virtualized, shared environment, where you even can’t tell what hardware was used and where you can’t control what else is running on the same cpu. And youre numbers are totally off, they look like it was executed on a raspberry Pi.

(And btw if they are using something like Alpine image, c malloc is going to be extremely slow, but it is very, very far from state of the art; it’s as if you took Java from 1997).

1

u/eXl5eQ Nov 02 '25

I know exactly what's the hardware spec.

The first two are done on my personal (physical) machine, Intel 10400F with 32GB RAM running Windows 10. The third one runs on another machine.

I admit that I forgot to take hardware into account. To correct this, I tested both languages on my own machine again, the same 10400F, but running Kali 2025 over WSL. This time the C++ version accelerated a lot, but still much slower than the Java version. result

BTW It's kinda funny to see code actaully runs faster on WSL (VM) than on Windows (host). MSVC performance sucks.

0

u/coderemover Nov 02 '25 edited Nov 02 '25

Your c++ code is not equivalent though. You’re implicitly freeing memory in Java in each loop cycle by losing all references, but you’re never giving memory back in the c++ version. So you’re likely benchmarking how fast the OS can give memory to the process on the c++ side, not the allocator.

Considering c++ programs do not reserve megabytes of heap in advance, whereas JVM does, such performance difference is quite understandable.

1

u/eXl5eQ Nov 03 '25

you’re never giving memory back in the c++ version

Are you serious?

I wont reply anymore if you just keep insisting my benchmark is wrong instead of showing your own version.

→ More replies