Zum Inhalt springen

Boxing in hot path (ARCH4087)

Dieser Inhalt ist noch nicht in deiner Sprache verfügbar.

Key java.performance.boxing-in-hot-path · Severity info · Category performance

Autoboxing primitives into wrappers like Integer/Long in a tight loop produces objects that burden the garbage collector every iteration. Primitive types avoid the boxing.

long sum = 0;
List<Long> values = new ArrayList<>();
for (int i = 0; i < n; i++) {
Long boxed = (long) i; // autoboxing each iteration
sum += boxed;
}
long sum = 0;
for (int i = 0; i < n; i++) {
sum += i; // primitive arithmetic, no boxing
}

Use primitive types (or IntStream/LongStream) instead of wrapper types in hot paths.