How can we find size of the object ?




Answers were Sorted based on User's Feedback



Answer / niranjanravi

there is no sizeOf() in java to find the size of the object

Is This Answer Correct ?    8 Yes 4 No

Answer / m!r@

public class FindSizeOfObject{
public static void main(String[] args) throws Exception{

Runtime obj = Runtime.getRuntime();
System.out.println(obj.totalMemory() - obj.freeMemory());
}
}

Is This Answer Correct ?    1 Yes 1 No

Answer / 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




Answer / sitaram

we don't have any method for to identify the size of object
in java.

Is This Answer Correct ?    0 Yes 2 No

Answer / suvarna

getObjectSize(Object objectToSize)
Returns an implementation-specific approximation
of the amount of storage consumed by the specified object.

Is This Answer Correct ?    0 Yes 3 No

Post New Answer



More Core Java Interview Questions

what is mean by ooad? where we are using? can you tell me any real time example?

1 Answers   Eka Software,


Can we override static methods?

18 Answers   Bally Technologies,


what are the differences between java and .net?..why u choose java?

1 Answers  


How to prevent a field from serialization ?

2 Answers  


Why can't we make jsp as a controller and action servlet in struts?

8 Answers   Wipro, TCS,


String is an immutable object. Then how can the following code be justified. String s1 = ?ABC?; String s1 = s1+?XYZ?; s.o.p(s1); The output is ABCXYZ, which is the value of s1 ?

6 Answers   Flextronics, Keane India Ltd,


Difference between the String and StringBuffer classes?

3 Answers  


what is the difference between java and j2ee... is that can i work j2ee on net beans IDE 6.0

2 Answers  


Differences between GridLayout and GridBagLayout?

1 Answers  


By what default value is an object reference declared as an instance variable?

1 Answers   Wipro,


how to handle http request in struts

2 Answers   Polaris,


what is platform

2 Answers   i3 Solutions,




Categories