Java 27 and 26: runnable demos and captured output for every JEP, plus version lanes
Co-Authored-By: Claude Sonnet 5 <[email protected]> Claude-Session: https://claude.ai/code/session_01B38FGKKam5SCGgwgduVAh3
This commit is contained in:
@@ -0,0 +1,74 @@
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import org.openjdk.jol.info.ClassLayout;
|
||||
import org.openjdk.jol.info.GraphLayout;
|
||||
import org.openjdk.jol.vm.VM;
|
||||
|
||||
/**
|
||||
* JEP 534 seen from the inside: how many bytes does each object spend on its header, and what
|
||||
* does that add up to across a million objects?
|
||||
*
|
||||
* <p>Uses JOL 0.17 ({@code org.openjdk.jol:jol-core}). Explained in docs/03-compact-headers.md.
|
||||
* Run by scripts/object-headers.sh, once per JDK.
|
||||
*/
|
||||
public class HeaderDemo {
|
||||
|
||||
/** Two ints. The smallest object that is not empty. */
|
||||
static class Point {
|
||||
int x;
|
||||
int y;
|
||||
}
|
||||
|
||||
/** A more typical mix: a long, an int, a boolean and a reference. */
|
||||
static class Order {
|
||||
long id;
|
||||
int quantity;
|
||||
boolean paid;
|
||||
String customer;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("java.version = " + System.getProperty("java.version"));
|
||||
System.out.println(VM.current().details());
|
||||
|
||||
section("java.lang.Object");
|
||||
System.out.println(ClassLayout.parseClass(Object.class).toPrintable());
|
||||
|
||||
section("Point { int x; int y; }");
|
||||
System.out.println(ClassLayout.parseClass(Point.class).toPrintable());
|
||||
|
||||
section("Order { long id; int quantity; boolean paid; String customer; }");
|
||||
System.out.println(ClassLayout.parseClass(Order.class).toPrintable());
|
||||
|
||||
section("java.lang.Long and java.lang.Integer (boxed values)");
|
||||
System.out.println(ClassLayout.parseInstance(Long.valueOf(123_456_789L)).toPrintable());
|
||||
System.out.println(ClassLayout.parseInstance(Integer.valueOf(123_456)).toPrintable());
|
||||
|
||||
section("int[3]");
|
||||
System.out.println(ClassLayout.parseInstance(new int[3]).toPrintable());
|
||||
|
||||
section("What it adds up to: one million objects");
|
||||
List<Integer> boxed = new ArrayList<>();
|
||||
List<Long> longs = new ArrayList<>();
|
||||
List<Point> points = new ArrayList<>();
|
||||
for (int i = 0; i < 1_000_000; i++) {
|
||||
boxed.add(1_000 + i); // above the Integer cache, so every one is a real object
|
||||
longs.add(10_000_000_000L + i);
|
||||
Point p = new Point();
|
||||
p.x = i;
|
||||
p.y = -i;
|
||||
points.add(p);
|
||||
}
|
||||
System.out.printf("1,000,000 boxed Integer values in an ArrayList : %,d bytes%n",
|
||||
GraphLayout.parseInstance(boxed).totalSize());
|
||||
System.out.printf("1,000,000 boxed Long values in an ArrayList : %,d bytes%n",
|
||||
GraphLayout.parseInstance(longs).totalSize());
|
||||
System.out.printf("1,000,000 Point objects in an ArrayList : %,d bytes%n",
|
||||
GraphLayout.parseInstance(points).totalSize());
|
||||
}
|
||||
|
||||
static void section(String title) {
|
||||
System.out.println();
|
||||
System.out.println("=== " + title);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,14 @@
|
||||
import org.openjdk.jol.info.ClassLayout;
|
||||
|
||||
/**
|
||||
* JOL 0.17 cannot lay out a record out of the box: it asks sun.misc.Unsafe for field offsets, and
|
||||
* Unsafe refuses for records. -Djol.magicFieldOffset=true switches JOL to a slower workaround.
|
||||
* Run by scripts/object-headers.sh; both outcomes are committed as transcripts.
|
||||
*/
|
||||
public class RecordProbe {
|
||||
record Coordinates(int lat, int lon) {}
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println(ClassLayout.parseClass(Coordinates.class).toPrintable());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user