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

What are methods in java?

529


What is flush buffer?

508


What is the list interface?

596


What are scalar data types?

515


What are different data types?

560






Why is a constant variable important?

489


What is java volatile?

530


What is collections framework?

566


What are runtime exceptions?

598


Why java is secure? Explain.

581


Explain the init method?

531


What is the purpose of a volatile variable?

544


When is the garbage collection used in Java?

648


Why is string class considered immutable?

593


What is difference between string and new string?

528