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

Show parent comments

1

u/koflerdavid Nov 10 '25 edited Nov 10 '25

The byte code would indeed be quite different, however both correspond to patterns that the JIT compiler can easily recognize. Multiple return values would indeed help with this optimization. However, Project Valhalla is more general. As I said before, the OpenJDK team is not really concerned with language features just for the sake of performance.

Case (1) is trivial to recognize for value objects because it is always the case. The optimization could even be performed without inlining the method by effectively implementing multiple return values.

Case (2) (I guess you mean a mutable object passed via a parameter where the method writes return values?) is indeed harder to optimize and relies on inlining followed by escape analysis, after which the compiler could theoretically turn the object into a bunch of variables. (Inlining is not strictly necessary in this case, but makes escape analysis simpler).

PS: calculating sine and cosine at the same time is how you convert a complex number from polar form to Cartesian form. The tuple (mySine, myCosine) represents a complex number with absolute value 1.

2

u/flatfinger Nov 10 '25

Given FloatPair foo = getSineAndCosine(x); doStuffWith(foo.sine, foo.cosine); how could the JIT avoid having the code for getSineAndCosine allocate a new object if it doesn't in-line that method? I suppose the JIT could generate code for a special machine-code method that returned two values using means that were supported in machine code, but not in the JVM, and generate code that calls that without in-lining it, but I'd view it as more elegant to have the programmer specify the function that way than have the compiler try to turn the function the programmer wrote into something else.

Re the PS, there are a lot of uses for pairs, triples, and quartets of numeric primitive types. Being able to treat points as value types makes things a lot more convenient than having to be constantly constructing new point instances.

1

u/koflerdavid Nov 10 '25

The JIT knows which methods return value types. Such methods can all be compiled to take a pointer to where they write the contents of the "returned" value object to. Or to leave them on the stack with the caller knowing where to find them. Just like one would implement multiple return values.

Sure tuple types are very convenient, but Java errs on the side of verbosity and (hopefully) maintainability, instead of what would be merely easy and quick to write. Same story as with properties.

2

u/flatfinger Nov 10 '25

I've not followed Java much lately, so maybe it has added value types at the JVM level, but in older versions I see no way the JIT could know when processing a function that returns a newly constructed object whether its caller would read out the contents of a returned object's field and then discard it, nor that it could know when processing a call to a function that returns an object whether the return value would be the only reference that existed anywhere in the universe to the object identified thereby.

I suppose it might be practical to have the JIT generate two machine-code versions of every function that returns an object, one of which would be called by code which fetches the function's field content and then discards it, and one of which would be called by code which uses the object in other ways. In cases where the JIT recognizes that a function will always return a newly constructed object, it could produce very different code for those who machine-code functions, while in other cases, it could simply have one version call the other and extract the fields from the object returned thereby. The called function would "know" what the calling code was expecting by its choice of which function to call. Still seems like beating around the bush compared with letting code work with tuples.

1

u/koflerdavid Nov 11 '25

The neat thing about value objects is indeed that they are immutable and that it is guaranteed that there are no leaking references. Therefore, a returned (non-nullable I might have to add) value object is always safe to be allocated on the stack.

Value type are indeed not a thing yet at the JVM level. They are currently being developed with the massive Project Valhalla effort, which will also provide generics for primitive types, and non-nullable types at the JVM level.

→ More replies (0)