Win a copy of Node.js Design Patterns: Design and implement production-grade Node.js applications using proven patterns and techniques this week in the Server-Side JavaScript and NodeJS forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Memory Footprint of ArrayList

 
Ranch Hand
Posts: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.



 
Sheriff
Posts: 26793
82
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 105
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 26793
82
Eclipse IDE Firefox Browser MySQL Database
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree, and also the choice: it implements collection API is Crookes.because it does implément list interface but also takes part of the collection API
 
I didn't say it. I'm just telling you what this tiny ad said.
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic