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 happens when heap memory is full?

522


Is hashset sorted in java?

658


How do you compare values in java?

538


What are the advantages of java over C++?

711


Explain scope or life time of local variables in java?

554






Does java runtime require a license?

574


What is the purpose of finalization in java programming?

557


How many types of java are there?

552


How does java enable high performance?

686


Can a constructor call the constructor of parent class?

532


What is a marker interface?

572


What is the purpose of return statement?

622


Explain the meaning of java applet.

598


What is the significance of java packages?

609


Hi.... I applied for the post of scientific officer/Engineer-SB(Programmer).Please post the syllabus and sample papers.

1506