0

I have a very simple application, we feed it a list of our websites, and it does a parallel foreach on them and inside each action it does an http post to it.

Similar to below:

static int success = 0
static void Main(string[] args) {
    try {
        Parallel.ForEach(sites, site=> {
            try{
                if(DoWebPost(site)) {
                    Console.Write("Posted {0} - {1}", ++success, site);
                }
            } catch {}
        });
    } catch {}
}

I can't figure out why it will throw an OutOfMemoryException, let alone why that will cause the entire application to crash, and not just catch and continue.

EDIT: Not exactly sure where the exception occurs, since it doesn't locally, only when running in production... meaning no debugging available. The Application is 64bits, and rarely uses more than 100 megs.

Jeremy Boyd
  • 5,245
  • 7
  • 33
  • 57
  • There are certain exceptions which cannot be caught. (If I remember correctly they are caught and and automatically rethrown and the mechanism is that the runtime sets a certain flag when throwing the exception the first time which indicates that it should be rethrown). Being out of managed memory is one of those. – CodesInChaos Sep 08 '11 at 13:19
  • 1
    How would you expect to continue from an out of memory error? – dlev Sep 08 '11 at 13:20
  • 1
    If you are out of memory, what should the runtime do? What would catching the exception help? Youre still out of memory, therefore the application crashes... – Philip Daubmeier Sep 08 '11 at 13:21
  • 1
    Not related to the exception, but you should use Interlocked.Increment to increment the static variable from different threads. – Henrik Sep 08 '11 at 13:23
  • Implement an event handler for AppDomain.Current.UnhandledException and log or display the value of e.ExceptionObject.ToString(). Post it here if that doesn't help diagnose it. – Hans Passant Sep 08 '11 at 13:43
  • @dlev and Philip - The other threads actually do continue while the one thread is out of memory, so the app continues, but after about 10 seconds there is a Aggregate exception that crashes the application. – Jeremy Boyd Sep 08 '11 at 13:43

2 Answers2

0

In the words of Marc Gravell

If you "fix" your code by handling this exception you are burying your head in the sand.

Community
  • 1
  • 1
spender
  • 117,338
  • 33
  • 229
  • 351
0

You mentioned the code that is posted is somewhat the same as the code you used. Can you actually post the full code or the exact code that is causing this. Something tells me you have some sort of memory leak, you need to address this first.

JonH
  • 32,732
  • 12
  • 87
  • 145
  • Unfortunately no, and it doesn't do it every time, even under the same parameters. What I do is just rerun the application, and it works, but I don't want to babysit it. – Jeremy Boyd Sep 08 '11 at 13:45