• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Finding out sizes of objects in memory

 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Sal Velinus
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 7610
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the info/links all!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic