1

I am using Parallel.ForEach to do my job but got "Out of Memory Exception".

Parallel.ForEach(flist, (item) =>
{
    string f1 = item.Split('|')[0];
    string f2 = item.Split('|')[1];
    a = File.ReadAllText(f1);
    b = File.ReadAllText(f2); 
    Consume(a, b);
});

flist's size is 351, a and b are string, each of them has 20kb size. At a certain time, the system memory blown up.

Consume return a string list, usually around 1000 strings in each iteration.

How to deal with it?

Chris Schiffhauer
  • 17,102
  • 15
  • 79
  • 88
  • What does Consume do? Is it memory intensive? – Baldrick Apr 06 '14 at 16:14
  • Did you check this post: http://stackoverflow.com/questions/6977218/parallel-foreach-can-cause-a-out-of-memory-exception-if-working-with-a-enumera?rq=1 – zindarod Apr 06 '14 at 16:14
  • setting maxdegreeofparallelism help? – Tim Apr 06 '14 at 16:14
  • It's very possible that you're creating 351 threads at once. Each has a stack of 1MB, plus the memory used by the file text and strings / lists etc. It's not inconceivable that it could run out of memory. Try using `ParallelOptions.MaxDegreeOfParallelism` set to 4, and see if that helps. – Baldrick Apr 06 '14 at 16:21
  • --@Baldrick, I am not sure how to add ParallelOptions.MaxDegreeOfParallelism in the code. Can you modify my code? –  Apr 06 '14 at 16:23
  • @Love: Added as answer below. – Baldrick Apr 06 '14 at 16:27

2 Answers2

3

Try replacing:

  Parallel.ForEach(flist, (item) =>
    {
        string f1 = item.Split('|')[0];
        string f2 = item.Split('|')[1];
        a = File.ReadAllText(f1);
        b = File.ReadAllText(f2); 
        Consume(a, b);
    });

With:

    Parallel.ForEach(flist, 
    new ParallelOptions { MaxDegreeOfParallelism = 4 },
    (item) =>
    {
        string f1 = item.Split('|')[0];
        string f2 = item.Split('|')[1];
        a = File.ReadAllText(f1);
        b = File.ReadAllText(f2); 
        Consume(a, b);
    });

This will prevent too many threads being created. Then you can always experiment with higher numbers and see if performance improves.

Baldrick
  • 11,712
  • 2
  • 31
  • 35
0

You're reading an entire file into a single string twice per loop. That's very likely the source of your problem if the file is large.

Martin Costello
  • 9,672
  • 5
  • 60
  • 72