Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Size of an Object

 
sreek, Thiru
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi, I want to know if there is any mechanism in java which gives the byte size of a arbitrary object....
something similar to sizeof() in C(!!!)
sreek
 
Roseanne Zhang
Ranch Hand
Posts: 1953
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there is none.
However, you can get not very accurate estimation by use
//=====================
Runtime rt = Runtime.getRuntime();
long before = rt.freeMemory();
Object o = new Object();
long after = rt.freeMemory();
long size = after - before;
//======================
However, if the class has String data members, the result will be very misleading. It can be inaccurate when JVM changed the memory allocation for some other reasons.
Thanks!
Roseanne
Join our SCJD Study Group when certified
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Not currently. Perhaps the best you can do is to use the freeMemory() and totalMemory() methods of the Runtime class - subtract one from the other to find out how much memory you're using at any one time. Do this once before you create your object, and once after, and compare the two. In theory this tells you how much memory the object takes up. However be aware that the API for these methods says that they return an approximation of the amount of free or total memory. Typically this means the number is rounded (up or down?) to the nearest multiple of 16 bytes. So to improve accuracy, you might try creating a large number of identical objects, and find the memory use for the group, then divide by the number of objects. Also, be sure to do a System.gc() each time before you find the memory used - you don't want the count thrown off by extraneous objects that haven't been deleted yet. (No, System.gc() doesn't really guarantee that these are deleted, but it usually does a pretty good job.)
Obviously, this is a lot more work, and less reliable, than the good old C sizeof operator, but that's what we're stuck with.
 
sreek, Thiru
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for ur help....
i don't think i can implement ur suggestions in my application... it would have been better if we had an easy way to do it.
anyway... thanks a lot
regards
Sreek
[This message has been edited by sreek, Thiru (edited February 22, 2001).]
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Out of curiosity, why do you need to know the size of an object for your application?
 
sreek, Thiru
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My application needs me to serialize some objects... but when deserializing instead of deserializing the entire file,i should be able to index into the serialized object file and deserialize a specific object. I thught of creating an index with object sizes.
sreek
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh. Well for starters, the number of bytes occupied by a serialized object in a file is not necessarily the same as the number of bytes it would occupy in memory. So Roseanne and I were answering a different question than the one you need. Offhand I'd say your best bet would be to count the bytes as they are written to an ObjectOutputStream. You can use a CheckedOutputStream to do this easily - create a Checksum class which simply counts bytes:
<code><pre>
import java.io.*;
import java.util.zip.*;

class ByteCounter implements Checksum {

private long count;

public long getValue() {
return count;
}

public void reset() {
count = 0;
}

public void update(byte[] b, int off, int len) {
count += len;
}

public void update(int b) {
count++;
}
}


public class Test {
public final static void main(String[] s) throws Exception
{
File file = new File("objects.ser");
OutputStream os = new BufferedOutputStream(
new FileOutputStream(file));
Checksum counter = new ByteCounter();
CheckedOutputStream cos = new CheckedOutputStream(os, counter);
ObjectOutputStream oos = new ObjectOutputStream(cos);

oos.writeObject("Testing...");
oos.flush();
System.out.println(counter.getValue());

oos.writeObject(new Double(1234.5));
oos.flush();
System.out.println(counter.getValue());

oos.writeObject(new int[1000]);
oos.flush();
System.out.println(counter.getValue());

oos.writeObject("another");
oos.flush();
System.out.println(counter.getValue());

oos.close();
}
}
</pre></code>
I'll leave the rest to you...

[This message has been edited by Jim Yingst (edited February 23, 2001).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic