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 the use of pojo (plain old java objects)? how it is interact with crystal reports? pls urgent

0 Answers  


What is the difference between constructor and method?

4 Answers  


what is the difference between Java Command Line and C command Line?

2 Answers  


string is immutable? right every one knows that, my question is it advantage or disadvantage making string immutable?

5 Answers   HCL,


What is design pattern and there types?

2 Answers   Cap Gemini,






1.) if we use "private" in place of "public" in public static void main()... 2.) if we use "int" in place of "void".... 3.) can we ommit "static" keyword from that statement.... 4.) also can we give the command line arguments type as int type or float,etc.(.i.e (string args[]))

1 Answers  


what is meant by package in java?

5 Answers  


what are the new features available in java 1.5 version?

3 Answers   Accenture, Motorola, Features,


What is deadlock and how to avoid this?

2 Answers  


What is a resource leak ?

1 Answers  


What is difference between static method and static variable?

7 Answers  


can we take more than one null values for Unique constraints.

1 Answers   3i Infotech,






Categories