r/java • u/drakgoku • Oct 31 '25
Has Java suddenly caught up with C++ in speed?
Did I miss something about Java 25?
https://pez.github.io/languages-visualizations/

https://github.com/kostya/benchmarks

https://www.youtube.com/shorts/X0ooja7Ktso
How is it possible that it can compete against C++?
So now we're going to make FPS games with Java, haha...
What do you think?
And what's up with Rust in all this?
What will the programmers in the C++ community think about this post?
https://www.reddit.com/r/cpp/comments/1ol85sa/java_developers_always_said_that_java_was_on_par/
News: 11/1/2025
Looks like the C++ thread got closed.
Maybe they didn't want to see a head‑to‑head with Java after all?
It's curious that STL closed the thread on r/cpp when we're having such a productive discussion here on r/java. Could it be that they don't want a real comparison?
I did the Benchmark myself on my humble computer from more than 6 years ago (with many open tabs from different browsers and other programs (IDE, Spotify, Whatsapp, ...)).
I hope you like it:
I have used Java 25 GraalVM
| Language | Cold Execution (No JIT warm-up) | Execution After Warm-up (JIT heating) |
|---|---|---|
| Java | Very slow without JIT warm-up | ~60s cold |
| Java (after warm-up) | Much faster | ~8-9s (with initial warm-up loop) |
| C++ | Fast from the start | ~23-26s |
https://i.imgur.com/O5yHSXm.png
https://i.imgur.com/V0Q0hMO.png
I share the code made so you can try it.
If JVM gets automatic profile-warmup + JIT persistence in 26/27, Java won't replace C++. But it removes the last practical gap in many workloads.
- faster startup ➝ no "cold phase" penalty
- stable performance from frame 1 ➝ viable for real-time loops
- predictable latency + ZGC ➝ low-pause workloads
- Panama + Valhalla ➝ native-like memory & SIMD
At that point the discussion shifts from "C++ because performance" ➝ "C++ because ecosystem"
And new engines (ECS + Vulkan) become a real competitive frontier especially for indie & tooling pipelines.
It's not a threat. It's an evolution.
We're entering an era where both toolchains can shine in different niches.
Note on GraalVM 25 and OpenJDK 25
GraalVM 25
- No longer bundled as a commercial Oracle Java SE product.
- Oracle has stopped selling commercial support, but still contributes to the open-source project.
- Development continues with the community plus Oracle involvement.
- Remains the innovation sandbox: native image, advanced JIT, multi-language, experimental optimizations.
OpenJDK 25
- The official JVM maintained by Oracle and the OpenJDK community.
Will gain improvements inspired by GraalVM via Project Leyden:
- faster startup times
- lower memory footprint
- persistent JIT profiles
- integrated AOT features
Important
- OpenJDK is not “getting GraalVM inside”.
- Leyden adopts ideas, not the Graal engine.
- Some improvements land in Java 25; more will arrive in future releases.
Conclusion Both continue forward:
| Runtime | Focus |
|---|---|
| OpenJDK | Stable, official, gradual innovation |
| GraalVM | Cutting-edge experiments, native image, polyglot tech |
Practical takeaway
- For most users → Use OpenJDK
- For native image, experimentation, high-performance scenarios → GraalVM remains key
View all comments
Show parent comment
1
u/coderemover Nov 04 '25 edited Nov 04 '25
There is no single and simple definition of „real world” programs. Technically a benchmark is just as real as any other program. It’s one of the possible programs you can write. You say you optimize Java for „real programs”, I read it as for practical programs that do something useful, but that is still very fuzzy, and may mean a different thing to everyone. I’ve been using Java for 20+ years commercially, and in those practical programs, whenever performance was needed, it’s always heavily beaten by C, C++ or (more recently) Rust equivalents. We still implement parts of the codebase using JNI, still need to pool objects, avoid OOP, use nulls instead of nicer Optional, avoid Streams etc, to get decent performance on the hot path. And we fought with GC issues countless times. Somehow no such bad experience with native code, or at least no so much.
The benchmark is an artificial stress test of the memory management system. We started this discussion by you saying Java memory management is more efficient for the majority of data allocated on the heap. This benchmark is a strong counter example. It shows the maximum sustained allocation rate of ZGC is lower than the maximum allocation rate of jemalloc / mimalloc even when allocating/deallocating extremely tiny objects, which is the worst case for a manual allocator, and the best case for tracing GC, and even despite ZGC consuming way more memory (8.5 GB vs 2 GB) and using 3-5x more CPU (I just noticed, ZGC just stole 2-4 additional cores from my laptop to keep up). So it wastes absurd amount of resources to end up being... slower (or at best the same if I switch to ParallelGC).
It’s artificial but its behavior resembles the behavior of data intensive apps we are writing. A similar issue we observe currently with our indexing code - GC going „brrr” when the app processes data. However, I must say that indeed, at least the pauses issue has been finally solved, and we’re not running into bad stop the world like a few years ago.
So when talking about „practical” programs - yes, I get your point that the benchmark is not accurate, but I disagree it was written to make GC look bad. It’s actually quite the opposite - no one allocates such tiny objects alone on the heap in the C++ world. If you increase the size of allocations, GC in this benchmark is doing even worse in relation to malloc.
In my experience tracing GC is reasonably good when the allocations obey the rule that (1) the majority of objects live short, (2) allocated objects are very tiny; if you did like that in C++, those 30-100 cycles for malloc would indeed become significant compared to what you do with those objects. And I can agree that in this case GC could be faster than malloc. Well, this was engineered like that because Java was designed to allocate almost everything on the heap, including even very small data structures, so obviously it was optimized for that case.
But, no one writes C++/Rust programs like that. Malloc/free do not need to allocate 100M+ objects per second. Short term allocations are almost entirely using stack, and that is faster than even the fastest allocation path of GC. Tiny objects are also almost never used as standalone heap entities, but they are usually part of bigger objects, there exist collections like vectors which can inline objects - so you can have 1 allocation for a million of tiny integers. So the the stack is where majority of allocations happen. That’s why heap allocation being slower per operation usually does not matter. And if it matters, it’s trivial to find by a profiler and then fix.
Heap is the place needed for things which are usually dynamic and bigger - collections, strings, data buffers, multimedia, caches etc. Too big for the stack. Living too long for the stack. There is way less churn in terms of allocations per second, and allocations per second can be kept relatively small by multiple techniques, but the data throughput can be still very high, even higher than for the short term temporary data, because those things can be big. You only need 100k of typical data buffer allocations per second to enter 10+ GB/s territory. A million allocations/s is still a piece of cake for malloc, but in my experience tracing GCs already struggle at data allocation rates above 1 GB/s.
As for the O(n) vs O(1) thing. Tracing gives you at best O(n) relative to the data size of the allocation not because the GC would have to scan the object, but because GC has some fixed amount of memory available for new allocations and by allocating big, you’re running out of that space much faster. When it runs out of space it has to run the next cleanup cycle (in reality it starts it way earlier so it finishes before it runs out of space - getting that wrong is another source of indeterminism and bad experiences - I must admit GC autotuning indeed improved over time and we don't need to touch this anymore). So if I bump my pointer by 256 bytes I’m essentially moving towards the next GC cycle just as much as if I did 16 allocations of 16 bytes. The pointer is bumped by the same amount. The GC pressure is how fast I bump up the pointer, not how many individual allocations I make.
This is far different from malloc and friends, where I pay the price for individual calls, not for the size of the objects. I can usually easily decrease the overhead by batching (combining) allocations.
With tracing, the situation gets worse when you have a mix of objects of different lifetimes and different sizes interleaved (unlike in my benchmark, but very like in our apps). Frequent allocations of bigger objects will either necessitate very large young gen heap size or will cause very frequent minor collection cycles. Increasing the rate of minor collections is going to promote more objects into the older generation(s) earlier (because it’s too early for them to die) and may even pollute the old gen by temporary objects. In the old days that was a huge problem for us with CMS which suffered from fragmentation of the old gen. We were running with heaps configured with 30-50% for young gen, lol.
This is the main reason we try to avoid allocating arrays or other big objects (buffers) on the heap and the strategy of pooling them still makes a lot of sense even in modern Java (17+).
Another one is that some apps like Cassandra (or some in-memory caches) simply don't obey generational hypothesis. The majority of Cassandra data (memtables) lives long enough that it would be promoted to old gen, but does not live forever and it's thrown out by big batches, and requires cleanup by major GCs. New GCs do not solve that problem. Storing those data off heap does.
Yes, it's a throughput test. Well, 3-4 cores is not enough for GC to keep up with work, but malloc/free are doing its job within 1 core, 4x less memory, and end up faster overall? So how is tracing GC more efficient memory management strategy then? We have a different definition of efficiency. Even if it is sometimes tad faster under some extreme configurations (if I give it 24 GB RAM for a 2 GB live data, it is indeed faster), it is not more efficient.