5 ms·
They are still classes, still live on the heap and still need to be garbage collected. Compare with value types that live on the stack in other languages such a
by jgrant27 5y ago
They are still classes, still live on the heap and still need to be garbage collected. Compare with value types that live on the stack in other languages such as Swift, Go and Julia.
- The_rationalist 5y agoValue classes, primitive types and specialized generics will be on the stack on the next versions. There is also this related work https://github.com/microsoft/openjdk-proposals/blob/main/stack_allocation/Stack_Allocation_JEP.md https://github.com/microsoft/openjdk-proposals/blob/main/sta...
- loukrazy 5y agoIsn’t the point that Record classes will be able to be upgraded to value types easily once Valhalla is done? Or am I missing something
- The_rationalist 5y agoNo they won't (or maybe they will be able to be speculatively opt-deopt?) Value types above a relatively small size are less efficient than references.
- kasperni 5y agoYes, just add 'primitive' before record in the declaration.
- kasey_junk 5y agoI’ve not explored them much, how well does escape analysis work with them?
- shagie 5y agoThe heap is an implementation detail. With escape analysis, the compiler can allocate the data on the heap, stack, or even stick it in registers. https://www.beyondjava.net/escape-analysis-java https://www.beyondjava.net/escape-analysis-java https://shipilev.net/jvm/anatomy-quarks/18-scalar-replacement/ https://shipilev.net/jvm/anatomy-quarks/18-scalar-replacemen... https://www.javaadvent.com/2020/12/seeing-escape-analysis-working.html https://www.javaadvent.com/2020/12/seeing-escape-analysis-wo...
- anonymoushn 5y agoCan you have an array of 1 million structs, not pointers to structs?
- shagie 5y agoThe first question is "through static analysis, can you guarantee that the structs do not leave the scope?" The second question to look at is "which JVM are you using?" Different JVMs may implement this differently. This isn't something that one can say about Java. It is something that one might be able to say about HotSpot, Zulu, or GraalVM.
- iainmerrick 5y agoYou’re technically correct that this stuff is all possible in principle, but the answer in practice right now is “no”.
- shagie 5y agoFrom the link about GraalVM: > Something that Graal can do that C2 cannot, and a key advantage of GraalVM, is partial escape analysis. Instead of determining a binary value of whether an object escapes the compilation unit or not, this can determine on which branches an object escapes it, and move allocation of the object to only those branches where it escapes. And from https://docs.oracle.com/en/java/javase/11/vm/java-hotspot-virtual-machine-performance-enhancements.html#GUID-6BD8FCB5-995B-4AE9-BFAA-B2C7DE2BA5CD https://docs.oracle.com/en/java/javase/11/vm/java-hotspot-vi... > The Java HotSpot Server Compiler implements the flow-insensitive escape analysis algorithm described in: > ... > After escape analysis, the server compiler eliminates the scalar replaceable object allocations and the associated locks from generated code. The server compiler also eliminates locks for objects that do not globally escape. It does not replace a heap allocation with a stack allocation for objects that do not globally escape. ---- So, some JVMs implement, others only do a limited subset of the optimizations available with escape analysis. I would not say that the answer of "is it used in practice" is "no."
- deleted 5y ago[deleted]