Docsity

Java 27 latest features, Apuntes de Tecnologías de la Información y la Comunicación

Toggle sidebar
bg1
Java 27 New Features | Part 3: JVM, Performance & Upgrade
Page 1
Java 27: JVM, Performance & Upgrade
Part 3 of 3. G1 everywhere, compact headers, the Vector API and a migration checklist.
JEP Feature Status Need a code change?
523 G1 is the default collector in all environments Final No
534 Compact object headers by default Final No
537 Vector API 12th incubator Opt-in API
1. G1 as the default everywhere (JEP 523)
Before JDK 27, the JVM chose the Serial collector on constrained machines (a single CPU, or less than about 1.8
GB of memory) and G1 otherwise. Now, if you name no collector, you always get G1. This matters for small
containers and Kubernetes pods, where behaviour could differ from your larger environments.
WhichGc.java
import java.lang.management.ManagementFactory;
public class WhichGc {
public static void main(String[] args) {
System.out.println("CPUs: " + Runtime.getRuntime().availableProcessors());
System.out.println("max heap MB: "
+ Runtime.getRuntime().maxMemory() / (1024 * 1024));
for (var gc : ManagementFactory.getGarbageCollectorMXBeans()) {
System.out.println("collector: " + gc.getName());
}
}
}
javac WhichGc.java
java -XX:ActiveProcessorCount=1 -Xmx512m WhichGc
JDK 27: one CPU, 512 MB heap
CPUs: 1
max heap MB: 512
collector: G1 Young Generation
collector: G1 Concurrent GC
collector: G1 Old Generation
Same, adding -XX:+UseSerialGC to keep the old collector
CPUs: 1
max heap MB: 511
collector: Copy
collector: MarkSweepCompact
What to do
•Compare latency, throughput and memory (RSS) of small services before and after upgrading.
•An explicit collector flag still wins. Serial remains available and is not deprecated.
•Log the choice at startup with -Xlog:gc.
•G1 flag changes: InitiatingHeapOccupancyPercent is deprecated in favour of G1IHOP; MinHeapFreeRatio and
MaxHeapFreeRatio now default to 0 and 100.
pf3
pf4

Vista previa parcial del texto

¡Descarga Java 27 latest features y más Apuntes en PDF de Tecnologías de la Información y la Comunicación solo en Docsity!

Java 27: JVM, Performance & Upgrade

Part 3 of 3. G1 everywhere, compact headers, the Vector API and a migration checklist.

JEP Feature Status Need a code change?

523 G1 is the default collector in all environments Final No

534 Compact object headers by default Final No

537 Vector API 12th incubator Opt-in API

1. G1 as the default everywhere (JEP 523)

Before JDK 27, the JVM chose the Serial collector on constrained machines (a single CPU, or less than about 1.

GB of memory) and G1 otherwise. Now, if you name no collector, you always get G1. This matters for small

containers and Kubernetes pods, where behaviour could differ from your larger environments.

WhichGc.java

import java.lang.management.ManagementFactory;

public class WhichGc { public static void main(String[] args) { System.out.println("CPUs: " + Runtime.getRuntime().availableProcessors()); System.out.println("max heap MB: "

  • Runtime.getRuntime().maxMemory() / (1024 * 1024)); for (var gc : ManagementFactory.getGarbageCollectorMXBeans()) { System.out.println("collector: " + gc.getName()); } } }

javac WhichGc.java java -XX:ActiveProcessorCount=1 -Xmx512m WhichGc

JDK 27: one CPU, 512 MB heap CPUs: 1 max heap MB: 512 collector: G1 Young Generation collector: G1 Concurrent GC collector: G1 Old Generation

Same, adding -XX:+UseSerialGC to keep the old collector CPUs: 1 max heap MB: 511 collector: Copy collector: MarkSweepCompact

What to do

  • Compare latency, throughput and memory (RSS) of small services before and after upgrading.
  • An explicit collector flag still wins. Serial remains available and is not deprecated.
  • Log the choice at startup with -Xlog:gc.
  • G1 flag changes: InitiatingHeapOccupancyPercent is deprecated in favour of G1IHOP; MinHeapFreeRatio and MaxHeapFreeRatio now default to 0 and 100.

2. Compact object headers by default (JEP 534)

Every object carries a header. On 64-bit HotSpot it shrinks from 96 bits (12 bytes) to 64 bits (8 bytes). The feature

was opt-in in JDK 25; JDK 27 turns it on by default. Applications with many small objects benefit most: DTOs,

records, map entries, cached rows.

Footprint.java

public class Footprint {

record Point(int x, int y) {}

public static void main(String[] args) { int count = 5_000_000; Point[] points = new Point[count]; // allocated before measuring long before = used(); for (int i = 0; i < count; i++) points[i] = new Point(i, i); long after = used(); System.out.printf("%.1f bytes per object%n", (after - before) / (double) count); System.out.println("last = " + points[count - 1]); }

static long used() { System.gc(); Runtime r = Runtime.getRuntime(); return r.totalMemory() - r.freeMemory(); } }

Measured on JDK 27 (Linux x64) $ java -XX:-UseCompactObjectHeaders -Xms512m -Xmx512m Footprint 24.0 bytes per object last = Point[x=4999999, y=4999999] $ java -XX:+UseCompactObjectHeaders -Xms512m -Xmx512m Footprint 16.0 bytes per object last = Point[x=4999999, y=4999999]

Here each Point holds two ints (8 bytes). With a 12-byte header the object needs 20 bytes, padded to 24. With an

8-byte header it fits exactly in 16: one third less for this deliberately favourable case. Real applications save less.

Reported by the JEP (SPECjbb2015 and a JSON parser benchmark) Result

Heap usage, in one SPECjbb2015 setting 22% lower

CPU time, in the same kind of setting 8% lower

GC count with G1 and Parallel, in another setting 15% fewer

Highly parallel JSON parser benchmark 10% less time

  • Opt out with -XX:-UseCompactObjectHeaders. The release notes say this option is planned for deprecation and removal.
  • Re-check heap sizing, container limits and any tool that assumes a 12-byte header (object-layout or heap-analysis agents).

Output (JDK 27) scalar = 9006. vector = 9006. [50, 155, 100, 106, 7]

  • Good candidates: numeric arrays, similarity scoring, checksums, image or signal processing. It does little for I/O-bound services.
  • Floating-point reductions can differ slightly from a scalar loop because the order of addition differs. These sample values are small whole numbers, so both results match.
  • Benchmark with JMH before adopting; the JIT already auto-vectorises some simple loops. 4. Upgrade notes: changes that can break an application

These come from the JDK 27 release notes rather than the nine JEPs.

Change What to check

Options removed: -noverify, -Xverify:none, -noclassgc,

-verifyremote

Start scripts, Dockerfiles, IDE run configs. An unknown

option stops the JVM

ThreadPoolExecutor.finalize() removed Subclasses overriding or calling it may not compile

JVMCI and -XX:+UseGraalJIT removed Remove those flags

UseCompressedClassPointers is obsolete Remove it; it is ignored with a warning

ServiceLoader wraps linkage errors in

ServiceConfigurationError

Optional plugins or providers with missing classes

HttpServer matches path segments by prefix /foo no longer matches /foobar

GZIPInputStream calls available() again between members Custom streams that feed compressed data

JSON thread dumps use numbers for IDs and counts Scripts or monitoring that parse them

ZipOutputStream throws ZipException for unmappable

names

Code catching IllegalArgumentException

CLDR 48.2 locale data Tests that compare formatted dates or currency

TLS and JFR defaults See Part 2

5. Migration checklist for a Spring Boot service

# Step

1 Decide the target. For long-lived production services, JDK 25 (LTS) is usually the safer base; use 27 if your team

follows six-month releases.

2 Confirm your Spring Boot version, build plugins and bytecode tools (Mockito/Byte Buddy, JaCoCo, agents) support

Java 27 class files (major version 71).

3 Check your JDBC driver (for example Oracle), Kafka clients, Avro and APM agent against vendor support statements.

4 Search start scripts and manifests for removed or deprecated JVM options.

5 Run jdeps --jdk-internals and the full test suite on JDK 27.

6 Load test: GC pauses, RSS, startup time, TLS connections to brokers, databases and gateways.

7 Keep preview and incubator APIs out of production code unless you accept changes at the next upgrade.

Sources: JEP 523: G1 as the default in all environments | JEP draft: Compact object headers by default | Oracle: JDK 27 release notes | Inside Java: JEP 537 targeted to JDK 27