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


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 (5)

  1. Marcel says:

    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.

  2. radzaw says:

    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…

  3. Justin says:

    This is happening for me also – any news on a fix?

  4. Douw says:

    We have the same issue. Have been hunting this issue for months now.

    Does anyone have a link to the Microsoft issue?

  5. Sebastian says:

    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 GetSystemInfo
    2. Increase allocation granularity to let’s say 1MB
    3. Profit!
    4. (optional) (no seriously, please do that) cleanup your detour after creating the AppDomain

    see example code here ( I used Process.NET for the detour, it’s quick and easy)


Skip to main content