Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

ArrayList Vs Array  RSS feed

 
Subhro Chowdhury
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I am storing 1000 objects (of same type) in an Array or in an ArrayList, which one will consume more space?
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can test it yourself:

Runtime.getRuntime()
- totalMemory ()
- freeMemory ()

What is your conclusion?


My guess is that the array will win (slightly)...

/Svend Rost
 
Campbell Ritchie
Marshal
Posts: 55722
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Subhro Ghowdury, welcome to the Ranch.

Svend Rost, have you tried it yourself?
 
Sidd Kulk
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I guess that Array should win, because ArrayList internally uses Arrays after performing few modifications on it. Though, in case of an Array, you can store Objects or Primitives of same type only, and Arrays cannot grow dynamically.
 
Subhro Chowdhury
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your responses!!
I have tried using
Runtime.getRuntime()
- totalMemory ()
- freeMemory ()
It seems ArrayList is taking more spaces.
Thanks!!
 
Subhro Chowdhury
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Could you please tell me whether Arrays are allocated contiguously in JAVA like in 'C'?
 
Gavin Tranter
Ranch Hand
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Subhro Chowdhury:
Could you please tell me whether Arrays are allocated contiguously in JAVA like in 'C'?


I would think that would be dependent on the JVM as the JVM does all the memory management, you cant "play" with the memory space like you can in C so you shouldnt need to worry about it. At least I never had, and if i am honest I have never missed it.
 
pete stein
Bartender
Posts: 1561
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hm I'm getting a greater than 4 fold difference in memory allocation for an array of 1000 Integer objects initialized to 0 vs an array list of 1000 Integers initialized to 0. Can this be?

The first takes up 13072 bytes, the second 54912.
 
Peter Chase
Ranch Hand
Posts: 1970
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Svend Rost:
Runtime.getRuntime()
- totalMemory ()
- freeMemory ()


Neither this, nor any other method, will reliably tell you exactly how much memory has been used. At best, it is an estimate. At worst, it can be completely wrong. Java simply does not specify how big objects are in real memory or provide any way to measure them.

A better way to work out the difference is to look at the JDK source for java.lang.ArrayList.

An ArrayList of the exact correct size - i.e. constructed as new ArrayList(1000) - will contain an object array of size 1000 internally. One would expect that to take the same amount of memory as a directly-allocated array, although there is no guarantee of this. The ArrayList then has two private fields of its own, plus the overhead of the ArrayList object itself. Therefore, one would expect the ArrayList to take a few tens of bytes more than the directly-allocated array. Again, there is no guarantee of this.

If the ArrayList was not constructed with the exact correct size, it is likely that its internal array will be noticably bigger than 1000, as it will have been increased in size via a well-defined sequence of sizes. Look at the source of ArrayList.ensureCapacity() to see how this happens.
[ April 19, 2007: Message edited by: Peter Chase ]
 
Campbell Ritchie
Marshal
Posts: 55722
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, have you tried it. I did.




































Sometimes I got a bigger size with ArrayList and sometimes with array
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell - try running this before each measurement:

You should find that the ArrayList size is always greater than (or very nearly equal to) the array size. (Assuming we're talking about an array of reference type, not long[] or double[].) That's because an ArrayList has an Object[] array inside it, which is necessarily at least big enough to hold all the contents of the ArrayList. Plus it has a few extra fields in it, which you can see by looking at the source.

In the event you still observe a correctly-sized array which takes more memory than an equivalent ArrayList, remember that freeMemory() and totalMemory() are approximations. On my box they seem to generally be within 8 bytes of the "correct" number. Some rounding seems to occur in the measurement. Also of course the System.gc() is never guaranteed to completely clear all available memory.
 
Campbell Ritchie
Marshal
Posts: 55722
163
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I did, and with 1000 members it was slightly larger for ArrayList. And they were reference type, Strings, "Campbell 1", "Campbell 2", etc.
With 100000 members it is sometimes more with array, sometimes with List.

And the ten gc() calls are taking so long I can see the delay; I printed the time and it is in the region of 0.3 seconds.
 
Svend Rost
Ranch Hand
Posts: 904
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Subhro, im glad you worked out the result

@Cambell: No I did not, since I knew the outcome. I prefeer that the OP (in the beginners forum) solves their own question through guidance rather than having the result served on a plate. However, I should have included the comment that Peter made about the unreliability of the method in my post.

@ Sidd: Correct comment on the potential "disadvantage" of the array implementation (assuming you do not use some sort of inheritance). With respect to "Arrays cannot grow dynamically" that is not entierly true since you can do something like the following:


You might say: yes, but that is very inefficient.. true, but tell that to the Vector

/Svend Rost
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!