In a ASP.Net web application, when you have a stack intensive operation to run, a stackoverflow exception is thrown on IIS when the stack size crosses the IIS set limit of 256K. A regular winform application howe开发者_如何学JAVAver has a limit of 1MB. So the exception would not occur when running the same operation in a Winform application.
There is no recursion or any other code specific issues here.
There are ways to work around the problem like using EditBin on w3wp.exe, but it’s not supported.
Other option is to modify the actual code to reduce the size of locals and there by the stack size, which might involve significant design, code changes.
But the following approach solves the issue when running the Stack intensive operation on a separate thread, explicitly specifying the size of 1MB.
Thread thread = new Thread(() =>
{
RunStackIntensiveOperation(someObject);
}, 1048576);
thread.Start();
thread.Join();
I am going with this 3rd approach. However, I am curious to see if anyone else had similar scenario and ran across issues due to the "operation on separate thread" approach.
What are the possible issues that might occur if I run a separate thread? What are the things to be aware of?
Opinions, suggestions please...
There is a way how to change stack size back to 1 MiB or even more. You can use EDITBIT command. Here's the information how to do it.
On the other hand modifying IIS binaries namely w3wp.exe does not look good from deployment point of view.
Caveats with extra thread are performance related. Extra thread will consume 1 MiB of memory. If there are a lot of requests to your web app you can run out of memory sooner.
To reduce memory usage you can make a custom thread pool with threads that have stack size of the right size.
精彩评论