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

How we get some middle records in one table?

2 Answers   3i Infotech,


What is the purpose of setAutoCommit() ?

1 Answers   Google,


What is meant by Encapsulation? Can you write a class to explain encapsulation?

8 Answers   Ness Technologies,


how to use finalize()Method to resources

4 Answers   TCS,


explain Anonynous inner class?

6 Answers   TCS,


Explain about GridBag Layout?

1 Answers  


what is generics in jdk1.5?

2 Answers   Bally Technologies,


What is the difference between Byte stream and Charecter Stream?

2 Answers   Tarang Software,


I have an HashMap object, which has with key and value pair. It has 10 keys and values in that object. Now the question is I want insert new key and value in middle or any where in that list but not at the end or at the top. Is it possible or not. If yes how can we achieve this one?

2 Answers   Huawei,


Which are thin and thicK wrapper Class's in Java?

1 Answers  


What is the difference between Static and final?

1 Answers  


What is AppletStub?

1 Answers  




Categories