Re: Serizlize You Cannot Use for Object Size
moleskyca1@yahoo.com wrote:
So what is the way to compute the memory consumed by object? This is
hard to work with language that doesn't support this. Can anyone post
some code that work? Say you have this class what will is total memory
for each instance:
public class Goo implements Serializable {
public int one;
public boolean two;
public boolean three;
public double x;
}
This is something that programmer should be able to do on any
language. I cannot do it in java, but i am new. Can anyone do it?
AFAIK there is no general answer to "how large is an object" in Java, unless
one explicitly accounts for the time element.
"Memory consumed" makes most sense in a runtime context. Others have alluded
to the difficulty, for example, of correlating the size of a serialized
representation to any runtime impact. Let's grant that what we care about is
amount of memory consumed by an instance at runtime.
But runtime is an interval - a program is loaded, runs for a while then ends.
The envelope of that varies according to the complexity of the program, its
usage patterns, whether it's a server process and so on. During that
interval, the shape of a program varies wildly due to Java's dynamic nature.
Even individual objects of a class could be implemented differently at
different times during runtime. For that matter, the same instance can change
its memory footprint during its lifetime. Are you interested in the
instantaneous memory footprint, the mean memory consumption, its maximum?
Over a single instance's lifetime or aggregated for the lifetime of the class?
For that matter, the class itself might be garbage collected altogether during
the program's run. If it's something used only during program initialization,
it might have an instantaneous footprint that is egregious but has no negative
impact on the program's performance during normal operation after it's been
collected. Even during the init phase, hotspotting might inline the whole
thing and it would essentially disappear even while in use.
These factors make it difficult to give any kind of simple answer to your
question.
--
Lew