From a222727c3edb6ea96e368030ac81d342bcee588e Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Thu, 2 Jul 2020 13:16:53 +0430 Subject: [PATCH 1/3] Added the JOL Examples --- .../java/com/baeldung/objectsize/Course.java | 10 +++++ .../objectsize/ObjectSizeUnitTest.java | 40 +++++++++++++++++++ .../com/baeldung/objectsize/Professor.java | 25 ++++++++++++ 3 files changed, 75 insertions(+) create mode 100644 core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/Course.java create mode 100644 core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/ObjectSizeUnitTest.java create mode 100644 core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/Professor.java diff --git a/core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/Course.java b/core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/Course.java new file mode 100644 index 0000000000..cbe2eed47f --- /dev/null +++ b/core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/Course.java @@ -0,0 +1,10 @@ +package com.baeldung.objectsize; + +public class Course { + + private String name; + + public Course(String name) { + this.name = name; + } +} diff --git a/core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/ObjectSizeUnitTest.java b/core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/ObjectSizeUnitTest.java new file mode 100644 index 0000000000..f2ba748d84 --- /dev/null +++ b/core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/ObjectSizeUnitTest.java @@ -0,0 +1,40 @@ +package com.baeldung.objectsize; + +import org.junit.Test; +import org.openjdk.jol.info.ClassLayout; +import org.openjdk.jol.info.GraphLayout; +import org.openjdk.jol.vm.VM; + +public class ObjectSizeUnitTest { + + @Test + public void printingTheVMDetails() { + System.out.println(VM.current().details()); + } + + @Test + public void printingTheProfClassLayout() { + System.out.println(ClassLayout.parseClass(Professor.class).toPrintable()); + } + + @Test + public void printingTheCourseClassLayout() { + System.out.println(ClassLayout.parseClass(Course.class).toPrintable()); + } + + @Test + public void printingACourseInstanceLayout() { + String ds = "Data Structures"; + Course course = new Course(ds); + + System.out.println("The shallow size is :" + VM.current().sizeOf(course)); + + System.out.println(ClassLayout.parseInstance(course).toPrintable()); + System.out.println(ClassLayout.parseInstance(ds).toPrintable()); + System.out.println(ClassLayout.parseInstance(ds.toCharArray()).toPrintable()); + + System.out.println(GraphLayout.parseInstance(course).totalSize()); + System.out.println(GraphLayout.parseInstance(course).toFootprint()); + System.out.println(GraphLayout.parseInstance(course).toPrintable()); + } +} diff --git a/core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/Professor.java b/core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/Professor.java new file mode 100644 index 0000000000..e03fd49098 --- /dev/null +++ b/core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/Professor.java @@ -0,0 +1,25 @@ +package com.baeldung.objectsize; + +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; + +public class Professor { + + private String name; + private boolean tenured; + private List courses; + private int level; + private LocalDate birthDay; + private double lastEvaluation; + + public Professor(String name, boolean tenured, List courses, + int level, LocalDate birthDay, double lastEvaluation) { + this.name = name; + this.tenured = tenured; + this.courses = courses; + this.level = level; + this.birthDay = birthDay; + this.lastEvaluation = lastEvaluation; + } +} From 5aa4950b33ecf3b27bcee970f585a25669258ce5 Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Thu, 2 Jul 2020 13:17:28 +0430 Subject: [PATCH 2/3] Removing an Unused Import --- .../src/test/java/com/baeldung/objectsize/Professor.java | 1 - 1 file changed, 1 deletion(-) diff --git a/core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/Professor.java b/core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/Professor.java index e03fd49098..6cd421f273 100644 --- a/core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/Professor.java +++ b/core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/Professor.java @@ -1,7 +1,6 @@ package com.baeldung.objectsize; import java.time.LocalDate; -import java.util.ArrayList; import java.util.List; public class Professor { From 40a70e44ac15f2436b10b429dbf17bdf05bd6810 Mon Sep 17 00:00:00 2001 From: Ali Dehghani Date: Thu, 2 Jul 2020 14:26:40 +0430 Subject: [PATCH 3/3] Added the Java Agent Example --- .../baeldung/objectsize/InstrumentedSize.java | 11 +++++++++++ .../java/com/baeldung/objectsize/MANIFEST.MF | 1 + .../objectsize/ObjectSizeCalculator.java | 16 ++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/InstrumentedSize.java create mode 100644 core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/MANIFEST.MF create mode 100644 core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/ObjectSizeCalculator.java diff --git a/core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/InstrumentedSize.java b/core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/InstrumentedSize.java new file mode 100644 index 0000000000..54177caf30 --- /dev/null +++ b/core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/InstrumentedSize.java @@ -0,0 +1,11 @@ +package com.baeldung.objectsize; + +public class InstrumentedSize { + + public static void main(String[] args) { + String ds = "Data Structures"; + Course course = new Course(ds); + + System.out.println(ObjectSizeCalculator.sizeOf(course)); + } +} diff --git a/core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/MANIFEST.MF b/core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/MANIFEST.MF new file mode 100644 index 0000000000..5366581b2f --- /dev/null +++ b/core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/MANIFEST.MF @@ -0,0 +1 @@ +Premain-Class: com.baeldung.objectsize.ObjectSizeCalculator diff --git a/core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/ObjectSizeCalculator.java b/core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/ObjectSizeCalculator.java new file mode 100644 index 0000000000..9c1e4c96a0 --- /dev/null +++ b/core-java-modules/core-java-jvm-2/src/test/java/com/baeldung/objectsize/ObjectSizeCalculator.java @@ -0,0 +1,16 @@ +package com.baeldung.objectsize; + +import java.lang.instrument.Instrumentation; + +public class ObjectSizeCalculator { + + private static Instrumentation instrumentation; + + public static void premain(String args, Instrumentation inst) { + instrumentation = inst; + } + + public static long sizeOf(Object o) { + return instrumentation.getObjectSize(o); + } +}