Peace,
hmmm... Maybe it may help to share with all of y'all the intention of
finding the objects size. I'm creating this program for myself (and
for whoever may find the information derived pertinent) to compare the
various different flavors of the xml parsers available for Java like
JDOM, dom4j, XOM, Xerces, Crimson, etc... I want to see the amount of
time required to parse various XML files and the memory footpath the
created document takes in the memory. So far, it's been pretty easy to
indirectly calculate the parse time and the amount of memory the
complete parsing operation with Document has taken. Unfortunately,
these are not ABSOLUTE results, but still can be used when comparing
one xml parser to another.
To calculate the time, the start time and end time of the parse are
taken and the time to parse is derived. This is simple, it works, and
is fairly consistent.
To calculate the memory, the start JVM memory total and free memory are
taken and after the parse, the JVM total and free memory are again
take. From the two sets of memories the used memory is calculated of
before and after parse and these are then used to come to an
'approximation' of the total memory used by the parse. Unfortunately,
this doesn't seem to be so reliable as the time method above. There
seems to be many issues, for example, Garbage Collection and
allocation/deallocation of additional memory by the JVM at various
times. Though, at the end of the parse, after the memory has been
recorded, the gc() is run.
Each XML file is parsed about 5 times and it's interesting to see that
usually, the VERY first parse is the slowest while the later ones seem
faster. Does anyone have any more ideas on other ways to go about
testing the different parsers and getting results so as to make a
better comparison of the parsers to one another. Right now, I would
like to just compare the parsing abilities and have the requirement
fulfilled before testing the other feature the parsers provide.
A final thought, I believe someone has mentioned this, what about
making the class serializable and storing ONLY the created parsed
DOCUMENT to the Serialized File and then seeing the size of the created
file? It may not ABSOLUTELY represent the memory footpath of the
document, but, would it be closer than the other methods discussed thus
far? And, wouldn't the results be more consistent? At least, it may
make comparing the parsers' document's memory footpath to one another.
God Bless,
shree
Chris said:
Codedigestion said:
Is there not a way to build a method to ask directly - the object
created how much memory it's taking up?
No. Not in standard Java, and not in anything that runs on a standard JVM.
Why not ? I suspect that it's for two reasons: one is that the Java designers
didn't think of it -- which is understandable (for once), since I know of no
realistic language where you /can/ ask an arbitrary object how much space it
takes[*]. The other is that it is difficult, if not impossible, to provide a
useful answer.
Consider this code fragment:
String greeting = "hello";
String myth = s.substring(0, 4);
It would certainly be possible to ask the Strings referred to by 'greeting' or
'myth' how much space they took up. In a current 32-bit JVM from Sun, they
would each take up 24 bytes (8 bytes for each object header, 3 32-bit integer
fields, and 1 32-bit reference field). There is no way to ask those Strings
that, however (unless you use the debugging API); which is reasonable because
the information would not be very useful. Most people would expect the size of
a String to depend in some way on how many characters it contains -- but it
doesn't.
So, could Strings be clever and add up the sizes of their contained objects ?
No. They could certainly add in the size of the char[] array objects, but
that's no use because they doesn't know how much of that array is logically
"part of" the String itself, and how much is just shared with other Strings (or
StringBuffers, or StringBuilders). In the example, 'myth' and 'greeting' share
their internal char[] arrays -- they both refer to the same array, but they
"use" different sub-sections of it.
-- chris
[*] There may be some, of course, but I don't know of any. And, in case anyone
is thinking of C's sizeof operator:
void
someFunction(char *buffer)
{
/* now, how do you ask the buffer how big it is ? */
}