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
Give me some null interfaces in java?
What is an class?
Can I import same package/class twice?
What is final keyword in java? Give an example.
What does null mean in java?
How does hashmap work in java ?
Can we define private and protected modifiers for variables in interfaces?
What are the advantages of autoboxing?
What is a class reference?
Does string is thread-safe in java?
What is finally block?
If two threads have same priority which thread will be executed first ?
What are sets in java?
What is the main purpose of java?
List the features of java programming language.