wood burning stoves 2.0*
The moose likes Java in General and the fly likes Size of Objects in Memory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Size of Objects in Memory" Watch "Size of Objects in Memory" New topic
Author

Size of Objects in Memory

Amit Dubey
Greenhorn

Joined: Dec 09, 2003
Posts: 2
Hi,
Is there a way in which you could get the exact size of a Object in Memory.
May the Object be of type Java or a Class defined by me.
Can we use the byte stream to write and see the no of bytes written
Any Hints Guys ?
John Smith
Ranch Hand

Joined: Oct 08, 2001
Posts: 2937
Is there a way in which you could get the exact size of a Object in Memory.
There is no way of doing it directly (such as using calling sizeof() in C++), but you can do it indirectly. Instantiate a thousand of your objects, run GC a few times, and measure free memory. Then make your objects eligble for GC, run the collector again a few times, and take a new measurement of free memory. Take the difference and divide by 1000. It's crude and rough, but it will give you an idea.
David Harkness
Ranch Hand

Joined: Aug 07, 2003
Posts: 1646
Originally posted by amit@apollo Dubey:
Is there a way in which you could get the exact size of a Object in Memory.

Check out this article for ways to calculate it by hand yourself: JavaWorld Tip 130: Do you know your data size?
walter wang
Ranch Hand

Joined: Jun 02, 2001
Posts: 154
try this flag
java -Xrunhprof StartClass
then you could find a dump file in working directory


public class Walter { public boolean is_Working_Now (boolean is_boss_Coming) { return is_boss_Coming; }
Akshay Sri
Greenhorn

Joined: Apr 22, 2003
Posts: 7
Here is some code which can be used to find out the size of an object.
The only limitation is that the object's class should have a no-argumment constructor.
It uses the concept of creating N no of objects of the required type, invoking GC, finding out the used memory, making the N objects eligible for garbage collection, invoking GC again and finding out the used memory.
public final class ObjectSizeFinder {
/**
* First and only argument is the package-qualified name of a class
* which has a no-argument constructor.
*/
public static void main(String[] aArguments){
Class theClass = null;

if(aArguments.length==0){
System.out.println
("Please enter the fully qualified class name of the class to find its object size");
System.exit(0);
}

try {
theClass = Class.forName(aArguments[0]);
}
catch (Exception ex) {
System.err.println("Cannot build a Class object: " + aArguments[0]);
System.err.println("Use a package-qualified name, and check classpath.");
}
long size = ObjectSizer.findObjectSize( theClass );
System.out.println("Approximate size of " + theClass + " objects :" + size+" bytes");
}

/**
* Return the approximate size in bytes, and return zero if the class
* has no default constructor.
*
* @param aClass refers to a class which has a no-argument constructor.
*/
public static long findObjectSize( Class aClass ){
long result = 0;
//if the class does not have a no-argument constructor, then
//inform the user and return 0.
try {
aClass.getConstructor( new Class[]{} );
}
catch ( NoSuchMethodException ex ) {
System.err.println(aClass + " does not have a no-argument constructor.");
return result;
}
//this array will simply hold a bunch of references, such that
//the objects cannot be garbage-collected
Object[] objects = new Object[SAMPLE_SIZE];
//build a bunch of identical objects
try {
Object throwAway = aClass.newInstance();
long startMemoryUse = getMemoryUse();
for (int idx=0; idx < objects.length ; ++idx) {
objects[idx] = aClass.newInstance();
}
long endMemoryUse = getMemoryUse();
float approximateSize = ( endMemoryUse - startMemoryUse ) /100f;
result = Math.round( approximateSize );
}
catch (Exception ex) {
System.err.println("Cannot create object using " + aClass);
}
return result;
}
/// PRIVATE ////
private static int SAMPLE_SIZE = 100;
private static long SLEEP_INTERVAL = 100;
private static long getMemoryUse(){
putOutTheGarbage();
long totalMemory = Runtime.getRuntime().totalMemory();
putOutTheGarbage();
long freeMemory = Runtime.getRuntime().freeMemory();
return (totalMemory - freeMemory);
}
private static void putOutTheGarbage() {
collectGarbage();
collectGarbage();
}
private static void collectGarbage() {
try {
System.gc();
Thread.currentThread().sleep(SLEEP_INTERVAL);
System.runFinalization();
Thread.currentThread().sleep(SLEEP_INTERVAL);
}
catch (InterruptedException ex){
ex.printStackTrace();
}
}
}
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Size of Objects in Memory