Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Memory usage  RSS feed

 
Doco Todd
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've noticed that a field declared boolean, byte, char or int it takes up the same amount of memory (presumably, it's transformed into an int). Is this thing dependant of VM implementation?
Also I found out that if the field is an array then there is a difference whether the array contains ints, chars, bytes, or booleans. Does this mean that some sort of packing takes place within arrays? And, if so, arrays of ints are faster to work with?
 
Peter Haggar
author
Ranch Hand
Posts: 106
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is dependent on the VM implementation, but typically the storage required to represent a primitive type is no larger than the amount of storage required to hold its range of values. For a 32-bit machine architecture, this size is typically 4 or 8 bytes. Therefore, storing all primitive types takes 4 bytes each, with the exception of double and long, which each take 8 bytes. The smaller primitive types (short, byte, char, and boolean) are often represented more compactly when used in fields of objects or classes or as array elements. They are, however, typically promoted to an int and use 4 bytes of storage when stored on the stack or used in local variables.
Notice the use of "typically". You can't depend on all VMs compacting chars and bytes in classes or arrays, but some VMs will do this.
Peter Haggar
 
Scott Palmer
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe you are seeing something like the minimum size of the data area in a class is the size of an int? How are you doing your tests?
 
Doco Todd
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Scott Palmer:
Maybe you are seeing something like the minimum size of the data area in a class is the size of an int? How are you doing your tests?

Is there such a thing as a minimal size of a data area?
In my test I'm instantiating one large (2500000) array of int/byte/char/boolean and also the same array with instances of SimpleInt/Char/Byte/Array (where class SimpleInt contains just a n int field).
 
Scott Palmer
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It could depend on the VM. For instance an optimization might be to start every object on a 32bit boundary. This alignment could improve access and on some architectures it might even be required...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!