• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Memory Footprint of ArrayList

 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Memory Footprint of ArrayList

I have read in the book "OCA Java SE 7" by (Edward Finegan)
that an ArrayList implementation requires more overhead than an array.

Is the author of the following question (see below) saying, because an ArrayList has the ability to decrease its size,
its memory footprint is smaller than an array?

In other words, when the ArrayList shrinks (elements of the array are removed),
the amount of memory it would require would be less than that of the array.

Based on other examinations and what I have read an ArrayList definitely has more overhead than an array.

Question: In the specific case that an ArrayList shrinks in size, is the memory footprint of an ArrayList smaller than that of an array?
--------------------
Test Question:
Mention two benefits of using ArrayList over array in software development.

(Choose two options.)


A. Reduces memory footprint

B. Implements the Collection API

C. Is thread safe

D. Dynamically resizes based on the number of elements in the list

Answer A, D:
Correct answers are options A and D.
Option A and D are correct because an ArrayList supports dynamic arrays that can grow as needed. In Java, standard arrays are of fixed length.
After an array is created, they cannot grow or shrink, which means that you must know in advance how many elements an array will hold.
But, sometimes, you may not know until run time precisely how large of an array you need.
To handle this situation, the collections framework defines ArrayList.
In essence, an ArrayList is a variable-length array of object references.
That is, an ArrayList can dynamically increase or decrease in size.
ArrayLists are created with an initial size.
When this size is exceeded, the collection is automatically enlarged.
When objects are removed, the ArrayList size decreases.



 
Paul Clapham
Sheriff
Posts: 21551
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm not impressed by that comparison. Sure, an ArrayList with 1,000 elements uses more memory than an array with 1,000 elements. And almost certainly an ArrayList with 6 elements uses less memory than an array with 1,000 elements. The former comparison is somewhat useful, but the latter comparison is of no use at all as far as I can see. It certainly can't be generalized into any statement about memory usage.
 
Thomas Hauck
Ranch Hand
Posts: 95
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I obtained this question from an exam and based on what I have read,
an ArrayList has more overhead than an Array.

The author of this test does not differentiate between
a) memory overhead and
b) memory footprint.

I posted this question because I do not agree with option A
(From the list of 4 options).

Are you saying that you also disagree with option A?


Can any conclusions be made whether an ArrayList has a smaller memory footprint than an Array?

-----
 
Paul Clapham
Sheriff
Posts: 21551
33
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas Hauck wrote:Are you saying that you also disagree with option A?


If the author is saying unequivocally that an ArrayList "reduces memory footprint", then yes, I unequivocally disagree with that. And taken by itself, that looks like what he is saying.

But the rest of what you posted supports the interpretation "An ArrayList can use less memory than an array under certain conditions". It's unfortunate that the whole thing is so clumsily put together that it's wide open to the obvious interpretation which you and I both found.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic