By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,452 Members | 1,571 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,452 IT Pros & Developers. It's quick & easy.

ArrayList size of elements

P: n/a
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??
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 15 '05 #1
Share this Question
Share on Google+
4 Replies


P: n/a
NoLongerMicah <pj**********@ra.rockwell-dot-com.no-spam.invalid> wrote:
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...

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #2

P: n/a
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.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 15 '05 #3

P: n/a
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.
Posted Via Usenet.com Premium Usenet Newsgroup Services
----------------------------------------------------------
** SPEED ** RETENTION ** COMPLETION ** ANONYMITY **
----------------------------------------------------------
http://www.usenet.com
Nov 15 '05 #4

P: n/a
NoLongerMicah <pj**********@ra.rockwell-dot-com.no-spam.invalid> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 15 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.