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

How to provide security in java

2034


What is final modifier?

772


When do we use synchronized blocks and advantages of using synchronized blocks?

930


Which class is the superclass for all the classes?

789


what is bmg file and how to create that files?what will it contailn?

2158


What is the collection interface?

831


What are passing parameters?

809


Why does java have two ways to create child threads?

703


What is the replace tool?

796


Why are pointers not secure?

802


What classes of exceptions may be caught by a catch clause in java programming?

965


What is defined as false sharing in the context of multithreading?

794


What is return type in java?

852


How do you read a char in java?

727


What are the 4 types of research methods?

716