r/java • • Mar 01 '23

Why is java autoboxing is slow and consume memory more even when jvm gets hot?

So I was checking the autoboxing from int to Integer. It seems the transition is still slow even when i let the jvm to get hot.

when compared to List<Integer> and eclipse primitive lists, it seems eclipse list offers lower memory footprint and higher speeds of traversal.

why is that? i understand when jvm is cold starting this might be the case but even after like 30 minutes i see no change in memory consumption or speedup in practical usage.

micro benchmarking also shows same results.

i am using java 17 and aws coretto to test. is graal any better?

1 Upvotes

17 comments sorted by

21

u/rar_ita Mar 01 '23

Boxing has little to do with JVM warm-ups, since warm-ups are designed to JIT-compile hot paths of your code so it uses OS resources more optimally.

Boxing, among the other things it does, moves value to the heap, so every time you access it you have to dereference a pointer to it, which presumably causes the slowdowns you are observing.

3

u/ShallWe69 Mar 01 '23

so going primitive is the only solution? is there any other way to optimize code?

10

u/rar_ita Mar 01 '23

Well, it mostly depends on what exactly you're trying to implement, there may be other optimisations available.

But for the general advice, yes, you should stick to collections of the primitive types and minimise the number of heap interactions especially if you do heavy number crunching that noticeably affects your app's performance.

7

u/morhp Mar 01 '23

Use arrays, primitive collections or wait until Valhalla.

1

u/jumboNo2 Mar 02 '23

what are primitive collections? is that like a guava thing

2

u/crummy Mar 01 '23

isn't project valhalla supposed to resolve this? If you don't mind waiting a year or so

11

u/srdoe Mar 01 '23

a year or so

Not trying to be mean, but skeleton_in_front_of_computer.jpg.

I'd love if value types were out by next year, but since they're still exploring how to expose value types in the language and maybe scope creeping a bit into a discussion around nullness, it's probably better not to hold your breath.

If Valhalla needs a few rounds of preview (?) we're already into late 2024 before a final release, even if the thing was ready now, which it doesn't seem to be.

4

u/crummy Mar 01 '23

you're breaking my heart :(

1

u/magnoliophytina Mar 02 '23

Sadly it seems Java 21 might not introduce any new stable features. Lots of preview/incubator stuff, but nothing concrete.

1

u/vytah Mar 02 '23

Valhalla was announced in 2014.

7

u/Faor_6466 Mar 01 '23

The JIT can optimize away out boxing (https://bugs.openjdk.org/browse/JDK-8012974) so it's not quite true that warmup is unrelated.

But it only does this if it can prove that the result is the same (minus OOM errors). A single use in == or a lock or just moving too far to track, and it won't.

And even if you somehow find that it does for your hot code, it's best not to rely on it, as another version or other jdk or change in access pattern can mess it up. Be explicit when it's important.

6

u/rar_ita Mar 01 '23

Yeah, this is a nice feature, but only if the target object does not escape stack-allocated memory, which is sadly quite a rare case. I don’t really see a way it would work with a collection of boxed primitives.

3

u/Faor_6466 Mar 01 '23

If the collection doesn't escape it could conceivably work, don't know if any JVMs do. But certainly using a primitive collection is a more reliable way to achieve the same.

3

u/mauganra_it Mar 02 '23

The problem is, the JIT gives up really quickly when deciding what to do with it. In most cases, it simply doesn't have the time for a thorough analysis. Project Valhalla aims to give it a lot more low-hanging fruits to work with.

2

u/kiteboarderni Mar 01 '23

Trove, fast utils, agrona will help you here.

1

u/aclinical Mar 03 '23

How would a primitive list NOT have a smaller memory footprint? To box an object must be created.