The vallhalla project will hopefully bring lower cost objects (at the expense of giving up Object Identity and/or nullability)
Shipilev provides daily Valhalla builds, you can download from here :
Wanted to compare the difference in memory / speed of using the value and primitive types. Here is simple test program I used.
As Regular Object (With Identity and Nullability)
public class Main { // Ensure you are using the valhalla jdk build // run with this command : // javac -XDenablePrimitiveClasses Main.java // then // java -XX:+EnablePrimitiveClasses Main static public class Point { private final double x ; private final double y ; public Point(double x, double y) { this.x = x; this.y = y; } } public static void main(String[] args) { long start = System.currentTimeMillis(); Point[] myArray = new Point[10_000_000]; for (int i = 0; i < myArray.length; i++) { myArray[i]=new Point(2d,3d); } Runtime.getRuntime().gc(); printMemoryUsage(); System.out.println("Array length : "+myArray.length); System.out.println("Total time "+(System.currentTimeMillis() - start)+ " ms"); } private static void printMemoryUsage() { System.out.println("MB: " + (double) (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (1024*1024)); } }
If I run this above example (using regular java objects) these are the numbers I’m getting
java Main.java
MB: 346.2878112792969
Array length : 10000000
Total time 250 ms
Now if we change the above Point class by making a primitive class (giving up Identity and Nullability) . To make it easy to do this, they have provided temporary keywords
primitive to declare the class as primitive type (giving up identity and nullability)
value to declare the class as value type (giving up identity)
As Primitive Type
public class Main { // Ensure you are using the valhalla jdk build // run with this command : // javac -XDenablePrimitiveClasses Main.java // then // java -XX:+EnablePrimitiveClasses Main static public primitive class Point { private final double x ; private final double y ; public Point(double x, double y) { this.x = x; this.y = y; } } public static void main(String[] args) { long start = System.currentTimeMillis(); Point[] myArray = new Point[10_000_000]; for (int i = 0; i < myArray.length; i++) { myArray[i]=new Point(2d,3d); } Runtime.getRuntime().gc(); printMemoryUsage(); System.out.println("Array length : "+myArray.length); System.out.println("Total time "+(System.currentTimeMillis() - start)+ " ms"); } private static void printMemoryUsage() { System.out.println("MB: " + (double) (Runtime.getRuntime().totalMemory() - Runtime.getRuntime().freeMemory()) / (1024*1024)); } }
This are the results if Point is primitive class
java Main.java
MB: 158.47823333740234
Array length : 10000000
Total time 72 ms
By turning the Point class into primitive class, it uses less than half the memory and it is 3x faster
As Value Type ( using “value” instead)
MB: 347.8201599121094
Array length : 10000000
Total time 224 ms
Declaring class as value type didn’t yield any memory saving, but did make code run slightly faster.