I need to build a big list of strings and keep it in memory, however while building it an OutOfMemoryException is thrown. According to Resource Monitor I still have 1GB of memory available. I found this KB article addressing the issue, but it seems like it should have been fixed in framework 1.1 SP1 (I am using 3.5 sp1).
Can anybody shed some light on what happens behind the scenes? Does the .net framework limit how much memory can be used by a single process (on a 32 bit system)? If so I can see why, but what doesn’t make sense is that the application is only using 1.6GB and there is still ~1GB left to the system.
Edit – For those who asked here is some more in-depth information:
I have a List (Yeah, I could use something else, but I am just prototyping right now.), I generate a random string by doing a Guid.NewGuid().ToString(), and throw it in the list. What I am trying to do is generate a list with as many items as I can fit into it, and test different methods of looking up a specific one. My first guess was that some fragmentation is going on, but I dropped everything except the code below, and it still happens. I don’t think this little snippet could create a lot of fragmentation, but I’m probably wrong.
List<string> blah = new List<string>();
for (int i = 0; i < 50000000; i++)
{
blah.Add(Guid.NewGuid().ToString());
}
The problem is probably not that you don’t have the memory “available”, but more likely that you’ve fragmented the memory so much that when you try to add an item to the list, and it must be resized, no single block of available memory can hold it.
This will also lead to OutOfMemoryException.
How big is the list, in other words, how many strings do you have in it, exactly or roughly, when you get the exception?
And how are you populating the list? Do you know in advance the number of items you’re going to add? And if so, do you specify the capacity when you construct the list?
If you don’t know, would it be possible to figure that out, so you could specify that capacity?