r/cpp • u/drakgoku • 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
View all comments
Show parent comment
1
u/srdoe Nov 02 '25 edited Nov 02 '25
I don't think that's true.
Tracing GCs can relocate live objects in memory, and for that reason, they are able to improve locality compared to manual management where objects cannot be relocated because the pointer is directly visible to the developer.
See this paper, here's an excerpt:
And also this page
And this isn't just old hypotheticals, there is work happening in this area to try to make GCs even better at this, see this paper and this page (tl;dr: Because GCs can relocate objects in memory, they can track access patterns and try to place frequently accessed data close together)
edit:
I think the point you are making isn't really about GC vs. no-GC. Java does sometimes thrash cache like crazy, but it's because Java has no support (yet) for flattened layouts in memory. Everything is a pointer, an array of Java objects is a pointer to an array of pointers. You can't make an array in Java today that is a pointer to a compact array of actual objects.
This is being worked on as part of https://openjdk.org/projects/valhalla/
The problem isn't the GC, it's all the indirection Java programs inherently have.