ArrayList size of elements

  • Thread starter Thread starter NoLongerMicah
  • Start date Start date
N

NoLongerMicah

Does anyone know how the ArrayList works in regards to sizing of
elements? What I mean is if you add 1,000 byte values to an
ArrayList does that mean that the ArrayList is then 1,000 bytes long
- or does it store each byte as a generic "object" - thus storing it
as a 4 bytes each??
 
NoLongerMicah said:
Does anyone know how the ArrayList works in regards to sizing of
elements? What I mean is if you add 1,000 byte values to an
ArrayList does that mean that the ArrayList is then 1,000 bytes long
- or does it store each byte as a generic "object" - thus storing it
as a 4 bytes each??

Actually, you'd end up taking more than that - 4 bytes for the actual
value, 8 bytes (IIRC) object overhead, and 4 bytes for the reference in
the ArrayList. Basically, you don't want to be doing that...
 
To answer my own question - the byte value is "boxed" - therefore for
each byte you add an "object" and a "byte" to the heap (the "object"
is a reference to the "byte" value).

So an array instance of 1,000 "bytes" would occupy 5,000 bytes on the
heap.
 
Thanks for the clarification. I wasn't thinking - didn't realize that
they store the byte as a object as well as it's reference as an
object.
 
NoLongerMicah said:
To answer my own question - the byte value is "boxed" - therefore for
each byte you add an "object" and a "byte" to the heap (the "object"
is a reference to the "byte" value).

So an array instance of 1,000 "bytes" would occupy 5,000 bytes on the
heap.

You're assuming that the cost of an object is 4 bytes, and that the
byte can be stored on its own - what makes you think that? Each object
has an 8 byte header, and I believe the data itself is stored in 4 byte
blocks, so 1000 boxed bytes would actually take 12K of heap. That's
consistent with a short test program, too.
 
Back
Top