How can we find size of the object ?

Answer Posted / lachha g

you can use Instrumentation class.

use long getObjectSize(Object objectToSize) method.
It returns amount of storage consumed by the specified object.
package Test;

import java.lang.instrument.Instrumentation;

public class ObjectSize {
private int x;
private int y;

ObjectSize(int x, int y){
this.x=x;
this.y=y;
}


public static void main(String [] args) {
ObjectSize obj= new ObjectSize(2,4);
System.out.println(Test.getObjectSize(obj));
}
}
class Test {
private static Instrumentation instrumentation;

public static void premain(String args, Instrumentation inst) {
instrumentation = inst;
}

public static long getObjectSize(Object o) {
return instrumentation.getObjectSize(o);
}
}

Is This Answer Correct ?    0 Yes 0 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Is java an open source?

731


Can a constructor have different name than a class name in java?

832


What is the function of java?

742


Difference between throw and throws?

874


Define array. Tell me about 2-D array.

818


How do you ensure that n threads can access n resources without deadlock?

1082


Is 0 an even number?

750


Is empty set an element of empty set?

837


What is this keyword in java?

798


How do you do absolute value in java?

732


What is byte code and why is it important to java’s use for internet programming?

818


Is double bigger than float?

749


What is a treeset class?

762


How do you represent a space in regex java?

737


What about features of local inner class?

839