Skip to content

Commit 62c6f34

Browse files
cdjolepivovarit
authored andcommitted
BAEL-1865 - Java Objects Sizes (eugenp#4584)
* BAEL-1865 - Java Objects Sizes * BAEL-1865 - PR fix
1 parent 335bd47 commit 62c6f34

3 files changed

Lines changed: 78 additions & 0 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.baeldung.objectsize;
2+
3+
import java.lang.instrument.Instrumentation;
4+
5+
public class InstrumentationAgent {
6+
private static volatile Instrumentation globalInstrumentation;
7+
8+
public static void premain(final String agentArgs, final Instrumentation inst) {
9+
globalInstrumentation = inst;
10+
}
11+
12+
public static long getObjectSize(final Object object) {
13+
if (globalInstrumentation == null) {
14+
throw new IllegalStateException("Agent not initialized.");
15+
}
16+
return globalInstrumentation.getObjectSize(object);
17+
}
18+
}
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
package com.baeldung.objectsize;
2+
3+
import java.util.ArrayList;
4+
import java.util.List;
5+
6+
public class InstrumentationExample {
7+
8+
public static void printObjectSize(Object object) {
9+
System.out.println("Object type: " + object.getClass() + ", size: " + InstrumentationAgent.getObjectSize(object) + " bytes");
10+
}
11+
12+
public static void main(String[] arguments) {
13+
String emptyString = "";
14+
String string = "Estimating Object Size Using Instrumentation";
15+
String[] stringArray = { emptyString, string, "com.baeldung" };
16+
String[] anotherStringArray = new String[100];
17+
List<String> stringList = new ArrayList<>();
18+
StringBuilder stringBuilder = new StringBuilder(100);
19+
int maxIntPrimitive = Integer.MAX_VALUE;
20+
int minIntPrimitive = Integer.MIN_VALUE;
21+
Integer maxInteger = Integer.MAX_VALUE;
22+
Integer minInteger = Integer.MIN_VALUE;
23+
long zeroLong = 0L;
24+
double zeroDouble = 0.0;
25+
boolean falseBoolean = false;
26+
Object object = new Object();
27+
28+
class EmptyClass {
29+
}
30+
EmptyClass emptyClass = new EmptyClass();
31+
32+
class StringClass {
33+
public String s;
34+
}
35+
StringClass stringClass = new StringClass();
36+
37+
printObjectSize(emptyString);
38+
printObjectSize(string);
39+
printObjectSize(stringArray);
40+
printObjectSize(anotherStringArray);
41+
printObjectSize(stringList);
42+
printObjectSize(stringBuilder);
43+
printObjectSize(maxIntPrimitive);
44+
printObjectSize(minIntPrimitive);
45+
printObjectSize(maxInteger);
46+
printObjectSize(minInteger);
47+
printObjectSize(zeroLong);
48+
printObjectSize(zeroDouble);
49+
printObjectSize(falseBoolean);
50+
printObjectSize(Day.TUESDAY);
51+
printObjectSize(object);
52+
printObjectSize(emptyClass);
53+
printObjectSize(stringClass);
54+
}
55+
56+
public enum Day {
57+
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
58+
}
59+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Premain-class: com.baeldung.objectsize.InstrumentationAgent

0 commit comments

Comments
 (0)