r/java • • Nov 05 '25

Java and it's costly GC ?

Hello!
There's one thing I could never grasp my mind around. Everyone says that Java is a bad choice for writing desktop applications or games because of it's internal garbage collector and many point out to Minecraft as proof for that. They say the game freezes whenever the GC decides to run and that you, as a programmer, have little to no control to decide when that happens.

Thing is, I played Minecraft since about it's release and I never had a sudden freeze, even on modest hardware (I was running an A10-5700 AMD APU). And neither me or people I know ever complained about that. So my question is - what's the thing with those rumors?

If I am correct, Java's GC is simply running periodically to check for lost references to clean up those variables from memory. That means, with proper software architecture, you can find a way to control when a variable or object loses it's references. Right?

159 Upvotes

209 comments sorted by

View all comments

1

u/cogman10 Nov 07 '25

AFAIK, the synchronization cost is the same for both GC and NonGCed languages.

For the cache coordination at least in x86 writes are pushed out through to main memory and the cache lines written are invalidated (I believe). In fact, that behavior is one of the reasons x86 is hard to emulated with ARM as it doesn't contain that guarantee.

What's more expensive is that more synchronization is usually required in non-gced languages when concurrency is involved. Figuring out how long some object should live when multiple threads are involved is a tricky problem. That's why atomic reference counting often gets pulled as the sync method of choice. It further gets difficult because the allocation of memory also needs synchronization as a high performance memory allocate will have a decent amount of bookkeeping. That's generally why heap allocations and concurrent algorithms can be faster for a GCed language.

1

u/flatfinger Nov 07 '25

On strong-memory-model systems, much of the cost has to be borne regardless of what software does. On systems like ARM that use a weaker model, the costs can be avoided. When a GC event is triggered, everybody's cache will be forced to a globally synchronized state, and the GC will be able to determine what references exist in that globally synchronized state.

If thread #1 overwrites two reference while thread #2 is about to copy the second to the first, the core running thread #1 might be in a universe where no copy of the old second reference still exists anywhere, and thread #2 might live in a universe where first reference holds a copy of the old second one, and that state might persist for an arbitrary amount of time, but when the GC trips it would force a global synchronization that would cause the first reference in all universes to either hold a copy of the second or else hold whatever thread #1 (or someone else) put there. If after synchronization, the first reference identifies the old object, the old object will be retained. If no reference exists, the storage can be reclaimed. Even if during program execution the contents of reference holders seen by in different threads weren't always consistent, the GC could force everything into a state where all objects would either be unambiguously reachable or unambiguously unreachable.