Udostępnij za pośrednictwem


Unexpected Out of Memory Exception in .Net 4.5 Applications that uses Custom Stack Commit Size

Recently I worked with a developer who was seeing an Unexpected Out of Memory Exception from his .Net 4.5 Application that was using custom stack reserve and commit sizes for the application. He was setting the values in Visual Studio as shown below

clip_image001

This particular application had some code that create new AppDomain using AppDomain.CreateDomain() method. It was working without any issues on .Net 4.0 but started throwing Out of Memory Exception on invoking AppDomain.CreateDomain() ever since he upgraded this to .Net 4.5 framework.

The exception was thrown despite the process had plenty of system resources available for its execution. Digging into this, we found the issue was happening due to an optimization introduced in .Net 4.5, to reduce the size of the stack requested for various internal helper threads that the CLR creates. This leads to a scenario where the stack reserve for a thread becomes less than stack commit size when we set a value > 253952 for stack commit size. Microsoft has identified this as a bug and are working on it, though we don’t have an ETA for a public fix at this moment. Time being one can use a value    < 253952 for stack commit size as a workaround

Comments

  • Anonymous
    June 05, 2014
    Thanks a ton for this blog entry! This is exactly the problem I've been experiencing here and wow was it strange and confusing... but how on earth are people expected to debug this? Shouldn't there at least be a knowledge base article or anything? Have found nil so far, except this post. Anyway, thanks again.

  • Anonymous
    April 19, 2015
    I'm getting the same problem when loading C# library (which is creating new app domain) from C++ unmanaged. But have no idea how to fix it...

  • Anonymous
    January 26, 2016
    This is happening for me also - any news on a fix?

  • Anonymous
    February 25, 2016
    The comment has been removed

  • Anonymous
    September 09, 2016
    Hi, thanks for the article!I got the same error today and tracked it down a little bit inside clr.dll (still in 4.6.whatever)The problem seems to be fishy parameters when internal appdomain threads are created.Luckily, the function calls GetSystemInfo (kernel32) to check the allocation granularity.A quick and dirty fix for this issue:1. Detour GetSystemInfo2. Increase allocation granularity to let's say 1MB3. Profit!4. (optional) (no seriously, please do that) cleanup your detour after creating the AppDomainsee example code here ( I used Process.NET for the detour, it's quick and easy)http://pastebin.com/23xxpyjtRegards