Skip to content

Commit 5e61930

Browse files
committed
Added the JOL Examples
1 parent a6cbd51 commit 5e61930

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package com.baeldung.objectsize;
2+
3+
public class Course {
4+
5+
private String name;
6+
7+
public Course(String name) {
8+
this.name = name;
9+
}
10+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.objectsize;
2+
3+
import org.junit.Test;
4+
import org.openjdk.jol.info.ClassLayout;
5+
import org.openjdk.jol.info.GraphLayout;
6+
import org.openjdk.jol.vm.VM;
7+
8+
public class ObjectSizeUnitTest {
9+
10+
@Test
11+
public void printingTheVMDetails() {
12+
System.out.println(VM.current().details());
13+
}
14+
15+
@Test
16+
public void printingTheProfClassLayout() {
17+
System.out.println(ClassLayout.parseClass(Professor.class).toPrintable());
18+
}
19+
20+
@Test
21+
public void printingTheCourseClassLayout() {
22+
System.out.println(ClassLayout.parseClass(Course.class).toPrintable());
23+
}
24+
25+
@Test
26+
public void printingACourseInstanceLayout() {
27+
String ds = "Data Structures";
28+
Course course = new Course(ds);
29+
30+
System.out.println("The shallow size is :" + VM.current().sizeOf(course));
31+
32+
System.out.println(ClassLayout.parseInstance(course).toPrintable());
33+
System.out.println(ClassLayout.parseInstance(ds).toPrintable());
34+
System.out.println(ClassLayout.parseInstance(ds.toCharArray()).toPrintable());
35+
36+
System.out.println(GraphLayout.parseInstance(course).totalSize());
37+
System.out.println(GraphLayout.parseInstance(course).toFootprint());
38+
System.out.println(GraphLayout.parseInstance(course).toPrintable());
39+
}
40+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package com.baeldung.objectsize;
2+
3+
import java.time.LocalDate;
4+
import java.util.ArrayList;
5+
import java.util.List;
6+
7+
public class Professor {
8+
9+
private String name;
10+
private boolean tenured;
11+
private List<Course> courses;
12+
private int level;
13+
private LocalDate birthDay;
14+
private double lastEvaluation;
15+
16+
public Professor(String name, boolean tenured, List<Course> courses,
17+
int level, LocalDate birthDay, double lastEvaluation) {
18+
this.name = name;
19+
this.tenured = tenured;
20+
this.courses = courses;
21+
this.level = level;
22+
this.birthDay = birthDay;
23+
this.lastEvaluation = lastEvaluation;
24+
}
25+
}

0 commit comments

Comments
 (0)