{"id":833,"date":"2022-08-13T00:00:09","date_gmt":"2022-08-13T00:00:09","guid":{"rendered":"https:\/\/choudhury.com\/blog\/?p=833"},"modified":"2022-10-29T22:05:38","modified_gmt":"2022-10-29T22:05:38","slug":"experimenting-with-java-valhalla","status":"publish","type":"post","link":"https:\/\/choudhury.com\/blog\/2022\/08\/13\/experimenting-with-java-valhalla\/","title":{"rendered":"Experimenting with Java Valhalla"},"content":{"rendered":"\n<p>The vallhalla project will hopefully bring lower cost objects (at the expense of giving up Object Identity and\/or  nullability)<\/p>\n\n\n\n<p>Shipilev provides daily Valhalla builds, you can download from here : <a href=\"https:\/\/builds.shipilev.net\/openjdk-jdk-valhalla\/\">\n\n<div class=\"codecolorer-container text default\" style=\"overflow:auto;white-space:nowrap;\"><div class=\"text codecolorer\">https:\/\/builds.shipilev.net\/openjdk-jdk-valhalla\/<\/div><\/div>\n\n<\/a><\/p>\n\n\n\n<p>Wanted to compare the difference in memory \/ speed of using the value and primitive types.  Here is simple test program I used.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>As Regular Object (With Identity and Nullability)<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public class Main {\n\n    \/\/ Ensure you are using the valhalla jdk build\n    \/\/ run with this command :\n    \/\/ javac -XDenablePrimitiveClasses  Main.java\n    \/\/ then \n    \/\/ java -XX:+EnablePrimitiveClasses Main\n\n    static public class Point {\n        private final double x ;\n        private final double y ;\n\n        public Point(double x, double y) {\n            this.x = x;\n            this.y = y;\n        }\n    }\n\n    public static void main(String[] args) {\n\n        long start = System.<em>currentTimeMillis<\/em>();\n\n        Point[] myArray = new Point[10_000_000];\n        for (int i = 0; i &lt; myArray.length; i++) {\n            myArray[i]=new Point(2d,3d);\n        }\n\n        Runtime.<em>getRuntime<\/em>().gc();\n\n        <em>printMemoryUsage<\/em>();\n\n        System.<em>out<\/em>.println(\"Array length : \"+myArray.length);\n\n        System.<em>out<\/em>.println(\"Total time \"+(System.<em>currentTimeMillis<\/em>() - start)+ \" ms\");\n\n    }\n\n    private static void printMemoryUsage() {\n        System.<em>out<\/em>.println(\"MB: \" + (double) (Runtime.<em>getRuntime<\/em>().totalMemory() - Runtime.<em>getRuntime<\/em>().freeMemory()) \/ (1024*1024));\n    }\n\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>If I run this above example (using regular java objects) these are the numbers I&#8217;m getting<\/p>\n\n\n\n<p>java Main.java<\/p>\n\n\n\n<p class=\"has-text-align-justify\"><em>MB: 346.2878112792969<br>Array length : 10000000<br>Total time 250 ms<\/em><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>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 <\/p>\n\n\n\n<p><strong>primitive  <\/strong>to declare the class as primitive type (giving up identity and nullability)<\/p>\n\n\n\n<p><strong>value<\/strong> to declare the class as value type  (giving up identity)<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>As Primitive Type<\/strong><\/p>\n\n\n\n<pre class=\"wp-block-preformatted\">public class Main {\n\n    \/\/ Ensure you are using the valhalla jdk build\n    \/\/ run with this command :\n    \/\/ javac -XDenablePrimitiveClasses  Main.java\n    \/\/ then \n    \/\/ java -XX:+EnablePrimitiveClasses Main\n\n    static public primitive class Point {\n        private final double x ;\n        private final double y ;\n\n        public Point(double x, double y) {\n            this.x = x;\n            this.y = y;\n        }\n    }\n\n    public static void main(String[] args) {\n\n        long start = System.<em>currentTimeMillis<\/em>();\n\n        Point[] myArray = new Point[10_000_000];\n        for (int i = 0; i &lt; myArray.length; i++) {\n            myArray[i]=new Point(2d,3d);\n        }\n\n        Runtime.<em>getRuntime<\/em>().gc();\n\n        <em>printMemoryUsage<\/em>();\n\n        System.<em>out<\/em>.println(\"Array length : \"+myArray.length);\n\n        System.<em>out<\/em>.println(\"Total time \"+(System.<em>currentTimeMillis<\/em>() - start)+ \" ms\");\n\n    }\n\n    private static void printMemoryUsage() {\n        System.<em>out<\/em>.println(\"MB: \" + (double) (Runtime.<em>getRuntime<\/em>().totalMemory() - Runtime.<em>getRuntime<\/em>().freeMemory()) \/ (1024*1024));\n    }\n\n}<\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>This are the results if Point is primitive class<\/p>\n\n\n\n<p>java Main.java<\/p>\n\n\n\n<p><em>MB: 158.47823333740234<br>Array length : 10000000<br>Total time 72 ms<\/em><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>By turning the Point class into primitive class,  it uses less than half the memory and it is 3x faster<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><strong>As Value Type<\/strong>  ( using  &#8220;value&#8221;  instead)<\/p>\n\n\n\n<p><em>MB: 347.8201599121094<br>Array length : 10000000<br>Total time 224 ms<\/em><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p>Declaring class as value type didn&#8217;t yield any memory saving, but did make code run slightly faster.<\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>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 : https:\/\/builds.shipilev.net\/openjdk-jdk-valhalla\/ Wanted to compare the difference in memory \/ speed of using the value and primitive types. Here is simple test program I used. As &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/choudhury.com\/blog\/2022\/08\/13\/experimenting-with-java-valhalla\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Experimenting with Java Valhalla&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[3],"tags":[],"class_list":["post-833","post","type-post","status-publish","format-standard","hentry","category-java"],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/choudhury.com\/blog\/wp-json\/wp\/v2\/posts\/833","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/choudhury.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/choudhury.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/choudhury.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/choudhury.com\/blog\/wp-json\/wp\/v2\/comments?post=833"}],"version-history":[{"count":15,"href":"https:\/\/choudhury.com\/blog\/wp-json\/wp\/v2\/posts\/833\/revisions"}],"predecessor-version":[{"id":868,"href":"https:\/\/choudhury.com\/blog\/wp-json\/wp\/v2\/posts\/833\/revisions\/868"}],"wp:attachment":[{"href":"https:\/\/choudhury.com\/blog\/wp-json\/wp\/v2\/media?parent=833"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/choudhury.com\/blog\/wp-json\/wp\/v2\/categories?post=833"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/choudhury.com\/blog\/wp-json\/wp\/v2\/tags?post=833"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}