Size of an item in cache...

Is there a way to know the size (in bytes) of an item in cache? I am
trying to do some memory planning and would like to see how big some
items are that I have cached?

Thanks,
Curt

You will need to serialize it and do a byte count on it.

Something like this:

Create a new ObjectOutputStream with a ByteArrayOutputStream into its constructor. Then write the object to the stream and get the size out of the ByteArray:

bout = createObject(“java”,"java.io.ByteArrayOutputStream").init();
oos = createObject(“java”,“java.io.ObjectOutputStream”).init( bout );
oos.writeObject( theTargetObject );

// get size
bout.size();

Now, this gives you an object size in bytes which can be very accurate for memory criteria, but the memory space for an element is JVM dependent. This is only an estimation. Also, this is slow for big object graphs, so I would be hesitant to start serializing everyting in cache.

Luis F. Majano
President
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

Hmm, I guess my object is not serializable.

java.io.NotSerializableException

Curt

yea, then you have a problem sir :slight_smile:

Luis F. Majano
President
Ortus Solutions, Corp

ColdBox Platform: http://www.coldbox.org
Linked In: http://www.linkedin.com/pub/3/731/483
Blog: http://www.luismajano.com
IECFUG Manager: http://www.iecfug.com

What is the object? You might be able to make an educated guess if it is something simple like a resultset and you can count the average number of rows, columns, and data per column.

I would say your next best bet would be to analyze a heap dump with something like the Eclipse Memory Analyzer Tool (aka MAT) or swap your JRE for JRockit on your server and inspect the heap with Mission Control. In THEORY, you can find the object in the heap and calculate the retained size. What’s going to be tricky is making sure you’re getting the size of all the deep references, not just a shallow reference to the object.

I’ve never used either of those tools for this specific purpose, but it’s where I would start in your position to see what you can find.

Thanks!

~Brad

You could try Railo’s sizeOf()?

Aren't embedded complex objects handled by reference? For instance, if
objA contains a field which is a query, and so does objB, if those
were just simply assignments (no Duplicate etc), there's still only
one copy of the actual object, with references to it in both objA and
objB.

How does sizeOf handle this? Would sizeOf(objA) include the size of
that query? It seems that if it does, embedded objects would get
over-counted, once for each reference. If it doesn't, you'd have to do
your own accumulation of total size, avoiding dupes. Either way, it
doesn't seem easy to get an accurate size metric.

That basic issue holds true regardless of how you measure.

Dave