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){

public static void main(String [] args) {
ObjectSize obj= new ObjectSize(2,4);
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 the return type of a program?s main() method?

3 Answers  

How does access modifiers work?

1 Answers   Wipro,

what is sendredirect?

7 Answers   TCS,

all are saying java doesn't support multiple inheritance but by default Object class is super class for all the user defined classes and we can extend atmost one class so each class can extend more than one class so java supports multiple inheritance?i am confused with this,pls any one explain me.

4 Answers  

What is anonymous class?

2 Answers  

Java violates few objected oriented concepts. Can you explain in what way?

3 Answers   Ness Technologies,

What is the use of Getters and Setters method ?

4 Answers  

public class Base { public void myMethod(int a,intb) {} } // Uses myMethod and then hides it. public class DerivedOne extends Base { private void myMethod(int a,int b); } will this compile or not .yes or no. why

2 Answers  

what is the use of StringBuffer?

2 Answers   Accenture,

How to convert String into primitive datatype.

6 Answers  

abstract class A{ int list []=new int [100]; A(int x,int y){ rondom n=new rondom(); for (int i==;i<100;i++); list [i] int(y-x+1); abstract sort (); abstract binary sarch(int temp); class B extends A{

3 Answers  

What is Java Classloader?

1 Answers   Phantom Technologies,