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
String and stringbuffer both represent string objects. Can we compare string and stringbuffer in java?
Define inheritance?
What is binary tree in java?
What is the difference between import java.util.date and java .util?
What ide should I use for java?
How does java enable high performance?
What is difference between iterator and enumeration in java?
What is an empirical question?
Can a java program have 2 main methods?
Write the algorithm to check the number non-leaf nodes in a tree.
Define Multiprogramming and Multiprocessing in java.
What are the two ways to create a thread?
How do you call a reference in java?
What is functional interface in javatpoint?
what is the difference between Object Based Language and Object Oriented Language?