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.
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.
Because your object is serializable already - you can use the size() method on ObjectOutputStream to do this.
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).
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.
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:
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.
How about using a completely different approach - a network monitoring tool to measure the data being sent over the wire? Will this be reliable?