Variable size calculation

Just a quickie: when trying to calculate the maximum memory footprint of variables in my class, is this the correct assumption?


// one byte
byte value = 10;    

// 10 bytes maximum? Would this be zero-bytes until an element is initialised with a value?
byte[] values = new byte[10];    

// 10,000 bytes maximum? (9.7kb). Again, do the rules with the above array apply re: initialisation?
byte[][] valuesMany = new byte[100][100];    


Thanks for any advice offered here!

Afaik memory gets allocated as soon as the compiler comes to a ‘new’ statement (roughly).
What your are talking about is something called ‘lazyness’. It’s a feature of other languages, which try to only compute / allocate stuff, when the stuff is needed. Some languages are completely lazy, like Haskell, every value is lazy there, others allow specifying if you want your stuff to be lazy, for example scala, which has the ‘lazy’ keyword.

Anyways. One more thing I’d like to say:
Arrays are pointers to memory locations where the array saves it’s values.
So if you create an array like in your example the ‘values’, then the local variable ‘values’ gets an integer assigned, which says where to look for the data inside the memory.

This means if you allocate ‘values’ you actually allocate 10 bytes + 4 bytes for the integer pointer (on 32 bit JVM’s, 8 byte on 64 bit JVM’s).
(this is not true for your [icode]byte value = 10;[/icode] example, but true for [icode]Byte byteWrapper = 10;[/icode], but that’s another story)

If you have a so-called two-dimensional array, then you really have arrays inside an array. This means your ‘valuesMany’ looks like this actually, where each [ ] pair is an array (and with that also a pointer).

[ [0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0] [0 0 0 0 0 0 0 0 0 0] ]

What does that mean?
Each “[” is another pointer…:


byte[][] valuesMany = new byte[][] {
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },
new byte[] { 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }
}

(^^^ pseudocode ^^^)
See these lots of ‘new’ keywords? :wink:

In the end this means you have 10 * 10 bytes (of data) + 11 pointer in case of a 64 bit JVM this would be 10 * 10 + 11 * 8 byte = 188 bytes instead of the expected 100 bytes.

Thats it with todays lesson :smiley:

It’s actually worse than Matheus says, because an array is an object and has an object header overhead. See http://www.javaspecialists.eu/archive/Issue078.html for an old approach; or experiment with java.lang.System’s information on currently used memory.

Thanks for this detailed and thorough explanation. I guess I must have forgotten about the inevitable assignment of a pointer to the array’s memory address.

Presumably, arrays are contiguous in memory (i.e. only require a single address location and size terminator), so that would assume that the initial assignment pointer will always remain a fixed size (that being the size required to define a memory address) and never grow any larger with different sized arrays.

It’s a shame that many languages now adopt the ‘everything is an object’ methodology - especially with primitive types. I like bytes to stay as bytes and not come with the costly overhead of having additional object requirements clogging up memory space.

Thankfully, with the (relatively) massive amounts of memory available on modern systems, these concerns are fairly trivial. But I can never let that be an excuse for sloppy, wasteful programming practices - especially with fundamental, basic data types!

Thanks again for your helpful explanations!

A single dimensional array is fixed size and is a linear memory chunk. All multi-dimensional arrays are really arrays of references to arrays. I’m not sure what you’re saying about “everything is a object”…because java isn’t one of those languages.

He’s talking about arrays being classes too. So there is a [icode]int[].getClass()[/icode], actually. That means int[] has to store class information, etc. This is a known overhead from instances.

[Devil’s advocate mode] If you have the C expression:

int* array = malloc(sizeof(int)*n)

There’s heap memory manager overhead with the memory chunk similar in size to Java’s overhead. But really I was referring to the “… especially with primitive types. I like bytes to stay as bytes…”.