wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Finding out sizes of objects in memory Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Finding out sizes of objects in memory" Watch "Finding out sizes of objects in memory" New topic
Author

Finding out sizes of objects in memory

Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
How do you find out how much memory an object takes up in memory? I need metrics for calculating how much working memory I will need for a particular task. Even if these sizes are implementation dependant, that will still be useful as a rule-of-thumb measurement.
For example, how many bytes does an Integer take up? How about a Double? How about any arbitrary object whose members I can recursively ask how big they are?
Thanks


Rob
SCJP 1.4
Sal Velinus
Greenhorn

Joined: Mar 05, 2002
Posts: 25
I believe the built in types (int, double, etc), are supposed to be the same on all platforms. I'm sure there's a chart somewhere. Thinking In Java Bruce Eckel's online book should have it somewhere.
Don't know about class objects, though, sorry.
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
I believe the built in types (int, double, etc), are supposed to be the same on all platforms. I'm sure there's a chart somewhere.

Rob did not mean that, surely...
Anyway, try this link
It also has an interesting article on object pooling...

-anthony
[ April 03, 2002: Message edited by: Anthony Villanueva ]
Valentin Crettaz
Gold Digger
Sheriff

Joined: Aug 26, 2001
Posts: 7610
That's kind of tricky since it is implementation dependent, and thus, you might get different results with different JVM providers. But I guess, what counts to you is that you get that information no matter who the provider is.
I remember somebody asking that question a couple of months ago... Wait...
I can't find the discussion anymore but I swear it is somewhere...
In the meantime here is a piece of Java code http://www.sosnoski.com/Performance/Code/ObjectSize.java
I guess you can use it to discover the size of any object.


SCJP 5, SCJD, SCBCD, SCWCD, SCDJWS, IBM XML
[Blog] [Blogroll] [My Reviews] My Linked In
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18671
The primitive types have standard ranges specified - from which it is possible to deduce the minimum amount of memory required. However there is no guarantee that a given implementation won't use more than the minimum memory - usually if the system's word size is larger.
To find out experimentally how much space is taken by either primitives or objects on a given machine, I usually use the totalMemory() and freeMemory() methods of Runtime. The differentce between these is what we want, as it reprents used memory. Measure this quantity immediately before and after creating objects to see how much space they take. Note that these methods are approximate - they tend to be rounded off at some level. I usually get around this by creating an array with thousands of objects, and measuring its size. Be sure to measure memory usage of the empty array, as well as after it's loaded with objects. Also, it's a good idea to call System.gc() a couple times before each measurement, to minimize "noise".


"I'm not back." - Bill Harding, Twister
Rob Ross
Bartender

Joined: Jan 07, 2002
Posts: 2205
Thanks for the info/links all!
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Finding out sizes of objects in memory
 
Similar Threads
size of objects in session
to find the size occupied by arraylist in kilobytes - i want to find the size of an arraylist
Notifying a particular client
SOAP over FTP
Batch Processing