views:

302

answers:

6

I'm doing some performance tuning on my application and would like to know a good way of measuring the size of the object that I am sending over RMI.

The aim is to make my object leaner.

A: 

Because your object is serializable already - you can use the size() method on ObjectOutputStream to do this.

Fortyrunner
There is no size method on ObjectOutputStream - you might want to clarify what you mean.
McDowell
I meant on ByteArrayOutputStream - thanks for the correction.
Fortyrunner
Alas, looking into it, this mechanism is not reliable - see my post.
McDowell
+2  A: 

The answer depends on which RMI protocol you are using. RMI supports a native Java protocol called JRMP and CORBA (RMI-IIOP), as well as other vendor-specific protocols. Even JRMP does not use the usual Java serialization (see the section RMI Customizes the Serialization Algorithm).

McDowell
Ok, lets assume its Java native protocol. Now how we go about it? I am curious. To me, I guess just have a approximate value by knowing the size of the object using Instrument.getObjectSize(), or Runtime.freeMemory(). I don't think you can get the exact size, anyway.
Adeel Ansari
The amount of RAM an instance uses (Instrumentation.getObjectSize()) and the amount of data that will be transmitted over the network are unlikely to equate. Runtime.freeMemory() reports the amount of free RAM in the VM - I do not see the relevance.
McDowell
So, do you think it would not be of any hint? What then otherwise, is there no way known to you?
Adeel Ansari
A: 

A few month ago, I found this article Instrumentation Memory Counter at the Javaspecialist site, about calculating memory usage of Java objects. Perhaps it could be helpful for you.

Goran Martinic
A: 

the easiest way to do this is to put in some test code which writes the object to a file. then, look at the size of the file.

or, if you don't want to write to disk (and your objects are not going to blow out memory):

ByteArrayOutputStream bout = new ByteArrayOutputStream();
ObjectOutputStream oos = new ObjectOutputStream(bout);
oos.writeObject(theObjectIWantTheSizeOf);
oos.close();
System.out.println("The object size is: " + bout.toByteArray().length:
james
This is not enough - the class may be sent as well as the serialized form of its instance.
McDowell
How about doing it twice?
Adeel Ansari
+1  A: 

This is not enough - the class may be sent as well as the serialized form of its instance. The class should only be sent once and it will be the size it is on disk. Are you only sending the object once, or many times? If the latter the size of the class shouldn't be important.

Peter Lawrey
A: 

How about using a completely different approach - a network monitoring tool to measure the data being sent over the wire? Will this be reliable?

dogbane