Getting Object Size
- In many cases you want to optimize the object size.
- Java has
Instrumentation
interface to get the object size. - Instrumentation interface has the concept of
premain()
. Header
of the program consume 8 bytes.- Object size must be calculate in
multiply of 8
. Step 1
: First make a program in java
Since
JDK 1.5 Java
have a new feature to get size of the Object.
Steps to get Object size:
import java.lang.instrument.Instrumentation; class B { /* Header Info 8 Byte */ int x; //4 float y; // 4 long d; //8 String z; //4 } class C { } public class A { /* new concept */ public static void premain(String args, Instrumentation ins) { B obj = new B(); long size = ins.getObjectSize(obj); System.out.println("B Size is "+size); size = ins.getObjectSize(new C()); System.out.println("C size is "+size); String q ="test"; size = ins.getObjectSize(q); System.out.println("Q String size is "+size); } public static void main(String[] args) { System.out.println("Inside Main "); } }
Step 2
: make the Executable JAR file of that projectStep 3
: put the premain() entry in manifest file.Step 4
: Execute the JAR.