File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes How to find the Size Of an Object ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to find the Size Of an Object ?" Watch "How to find the Size Of an Object ?" New topic

How to find the Size Of an Object ?

Venkat Ramayanam

Joined: Apr 25, 2006
Posts: 5
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?

java.lang.instrument.Instrumentation.getObjectSize(Object objectToSize)
Rajah Nagur
Ranch Hand

Joined: Nov 06, 2002
Posts: 239
Check this out

You can't wake a person who is <b><i>pretending</i></b> to be asleep.<br />Like what <b>"it"</b> does not like - <i> Gurdjieff </i>
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
Why do you want to know the size of an object in memory? Java insulates the programmer from that sort of thing and 99% of the time, that's exactly what you want.

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
Pradeep Kadambar
Ranch Hand

Joined: Oct 18, 2004
Posts: 148
As Peter said

Why do you want to know the size of an object in memory?

There is no necessity for this. Many of us migrating form C/C++ to Java have the tendency to think in the same grounds as the former.

Here the articles clearly addresses the same issues.
Sizeof for Java

Data Size

I hope these articles will clear off your doubts.
Stan James
(instanceof Sidekick)
Ranch Hand

Joined: Jan 29, 2003
Posts: 8791
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
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
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.

I agree. Here's the link:
subject: How to find the Size Of an Object ?
It's not a secret anymore!