Hi, Pls could some one tell me how to find the size of an object?? I know that there is no sizeof operator in java,but is there any direct way of finding it out? Does the following code accomplish the task?
There are plenty of reasons to want to know this. In making a design choice I might prefer the one that takes less memory. I might have a good idea of how many objects I'll have and want to predict how much memory I'll need to buy. I might have an out of memory issue and want to know which object is causing it.
Unfortunately all those reasons don't help with the answer: we don't know.
We have used some tools from IBM that read verbose logs and give a report of object allocations - class name & size and so on. This helped us learn that a logger (for which we have no source) was making 5 or 6 copies of a many-megabyte log message (which we eliminated!), causing allocation errors in several unrelated areas of the app.
A good question is never answered. It is not a bolt to be tightened into place but a seed to be planted and to bear more seed toward the hope of greening the landscape of the idea. John Ciardi
Joined: Oct 30, 2001
If you need the size of an object for debugging memory problems, hprof will tell you the size, won't it? I thought the question was about getting the size programmatically within an ordinary application, which is something you can't do.
I've just been doing some hprof. Here's a little extract from my hprof output. You can clearly see the sizes (sz) of the objects and arrays.