|
| 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 | +} |
0 commit comments