Collections and memory

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
 
M

Mattias Sjögren

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
 
M

Michael D. Ober

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

Mike.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Top