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 difference between overloading and overriding?

2 Answers   Accenture,


What is abstraction with strong example program? (not a general program)

3 Answers  


Can a Byte object be cast to a double value?

3 Answers   KO,


Tell me the programme for this @ 1 2 @ @ @ 1 2 3 4

5 Answers   Value Labs, iGate, IntoNET, Accenture,


What is an abstract class and abstract method?

5 Answers  


why is S capital in System.out.println ????

6 Answers  


Java does not support multiple inheritance. how interface helps in the same?

5 Answers   TCS,


Program to print 1 1 2 1 2 3 1 2 3 4 like that

8 Answers   Huawei,


How are Observer and Observable used?

3 Answers  


if arraylist size is increased from initial size what is the size of arraylist...suppose initial is 100 , if i add 101 element what is the size...

10 Answers  


whats is inheritance?

15 Answers   CTS, HCL,


What are the operands of instanceof operator?

3 Answers  




Categories