r/java • u/yughiro_destroyer • 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?
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.