How can we find size of the object ?

Answers were Sorted based on User's Feedback



How can we find size of the object ?..

Answer / niranjanravi

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

Is This Answer Correct ?    8 Yes 4 No

How can we find size of the object ?..

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

How can we find size of the object ?..

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

How can we find size of the object ?..

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

How can we find size of the object ?..

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 will you calculate the depth of a binary tree if the tree contains 15 nodes?

0 Answers   Aricent,


What do you know about the garbage collector in java?

0 Answers   TCS,


If two threads have same priority which thread will be executed first ?

0 Answers  


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 this() and super() ?

0 Answers  


When do we use synchronized methods in java?

0 Answers  


What is a substitution variable?

0 Answers  


Why we use methods in java?

0 Answers  


Can we extend singleton class?

0 Answers  


What is identifier give example?

0 Answers  


What are the approaches that you will follow for making a program very efficient?

0 Answers  


Is special character in java?

0 Answers  


Categories