Collections and memory

  • Thread starter Thread starter billsahiker
  • Start date Start date
B

billsahiker

Is the capacity of collections, such as arraylist, limited by
available RAM, or do they use virtual memory, caching to disk when
needed? I tested this by writing a simple console application that
declares an arraylist and using a for loop that adds 75 million items,
each with the number 1. I get an outofmemory exception. It works with
60 million items, and takes 81 seconds. I have a new pc with 1 GB RAM,
HD with 150GB free. using framework 2.0, vs2005.

Bill
 
Is the capacity of collections, such as arraylist, limited by
available RAM, or do they use virtual memory, caching to disk when
needed?

They use virtual memory just like the rest of your applicaton, that's
a feature provided by the operating system.

But that doesn't change the fact that an ArrayList is backed by an
object array, which must exist in continuous memory and which must be
reallocated as the number of items grows. If there's no continuous
memory area large enough you'll get an OutOfMemoryException.


Mattias
 
If you use a LinkedList<t>, you can store more items. LinkedLists don't use
an array that has to be copied.

Mike.
 
Back
Top