JAVA-1522 Split core-java-modules/core-java module
This commit is contained in:
@@ -12,3 +12,4 @@ This module contains articles about working with the Java Virtual Machine (JVM).
|
||||
- [Guide to System.gc()](https://www.baeldung.com/java-system-gc)
|
||||
- [Runtime.getRuntime().halt() vs System.exit() in Java](https://www.baeldung.com/java-runtime-halt-vs-system-exit)
|
||||
- [Adding Shutdown Hooks for JVM Applications](https://www.baeldung.com/jvm-shutdown-hooks)
|
||||
- [How to Get the Size of an Object in Java](http://www.baeldung.com/java-size-of-object)
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
package com.baeldung.objectsize;
|
||||
|
||||
import java.lang.instrument.Instrumentation;
|
||||
|
||||
public class InstrumentationAgent {
|
||||
private static volatile Instrumentation globalInstrumentation;
|
||||
|
||||
public static void premain(final String agentArgs, final Instrumentation inst) {
|
||||
globalInstrumentation = inst;
|
||||
}
|
||||
|
||||
public static long getObjectSize(final Object object) {
|
||||
if (globalInstrumentation == null) {
|
||||
throw new IllegalStateException("Agent not initialized.");
|
||||
}
|
||||
return globalInstrumentation.getObjectSize(object);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
package com.baeldung.objectsize;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
public class InstrumentationExample {
|
||||
|
||||
public static void printObjectSize(Object object) {
|
||||
System.out.println("Object type: " + object.getClass() + ", size: " + InstrumentationAgent.getObjectSize(object) + " bytes");
|
||||
}
|
||||
|
||||
public static void main(String[] arguments) {
|
||||
String emptyString = "";
|
||||
String string = "Estimating Object Size Using Instrumentation";
|
||||
String[] stringArray = { emptyString, string, "com.baeldung" };
|
||||
String[] anotherStringArray = new String[100];
|
||||
List<String> stringList = new ArrayList<>();
|
||||
StringBuilder stringBuilder = new StringBuilder(100);
|
||||
int maxIntPrimitive = Integer.MAX_VALUE;
|
||||
int minIntPrimitive = Integer.MIN_VALUE;
|
||||
Integer maxInteger = Integer.MAX_VALUE;
|
||||
Integer minInteger = Integer.MIN_VALUE;
|
||||
long zeroLong = 0L;
|
||||
double zeroDouble = 0.0;
|
||||
boolean falseBoolean = false;
|
||||
Object object = new Object();
|
||||
|
||||
class EmptyClass {
|
||||
}
|
||||
EmptyClass emptyClass = new EmptyClass();
|
||||
|
||||
class StringClass {
|
||||
public String s;
|
||||
}
|
||||
StringClass stringClass = new StringClass();
|
||||
|
||||
printObjectSize(emptyString);
|
||||
printObjectSize(string);
|
||||
printObjectSize(stringArray);
|
||||
printObjectSize(anotherStringArray);
|
||||
printObjectSize(stringList);
|
||||
printObjectSize(stringBuilder);
|
||||
printObjectSize(maxIntPrimitive);
|
||||
printObjectSize(minIntPrimitive);
|
||||
printObjectSize(maxInteger);
|
||||
printObjectSize(minInteger);
|
||||
printObjectSize(zeroLong);
|
||||
printObjectSize(zeroDouble);
|
||||
printObjectSize(falseBoolean);
|
||||
printObjectSize(Day.TUESDAY);
|
||||
printObjectSize(object);
|
||||
printObjectSize(emptyClass);
|
||||
printObjectSize(stringClass);
|
||||
}
|
||||
|
||||
public enum Day {
|
||||
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
Premain-class: com.baeldung.objectsize.InstrumentationAgent
|
||||
Reference in New Issue
Block a user