I am new to the concept of memory division in programming. I have found that size of stack is in most cases in .NET applications 1MB round. My question is: "How doesn't the stack overflow appear when in some functions I use local variables for "Image" type that is bigger than 1MB. Thanks in advance.
5 Answers
Because a StackOverflow exception has nothing to do with stack or heap memory management. Per the MSDN Documentation:
The exception that is thrown when the execution stack overflows because it contains too many nested method calls. This class cannot be inherited.
Now, if you're talking about the stack as far as the memory is concerned, then we're in a different world. The images you're storing in memory are likely held on the Large Object Heap. Memory management, and the conversation of it, is much too broad for this forum - but if you have a specific question about memory management then we can address that.
It's important that you understand that you are mixing two nomenclatures, and concepts, in your question and that there is an explicit difference between the two. I don't want you to go on thinking that you should be getting a StackOverflow exception because of large objects. I also don't want you to go on thinking that you are getting a StackOverflow exception because of large objects and memory management.
5 Comments
StackOverflow exception has nothing to do with objects in memory. You can see that clearly from the MSDN documentation. However, if the OP is speaking of memory management then that's a different world. Please see my update.StackOverflowException occurs when the stack runs out of memory. You could, in theory, have just one or two method calls that contain 1MB worth of stack space, but in practice that's virtually impossible; the only way to do it is through lots of nested method calling. What the OP is missing is that the large object he's allocating isn't going on the stack, the stack just has a pointer to an object on the heap (or large object heap in this case).The image itself is not stored on the stack, it is stored on the heap. Only a pointer/reference to the image is kept on the stack, which is a lot smaller.
public static void DoSomethingToImage()
{
Image img = new Image(...);
}
In the above code fragment, the Image is allocated on the heap and a reference to the image is stored in the img variable on the stack.
Comments
The main reason for a stack overflow error to happen is to have a large count of functions calls, an example that would through this error would be like this:
static int x = 0;
static void Main()
{
fn();
}
static void fn()
{
Console.WriteLine(x++);
fn();
}
This happens due to something wrong at the code, because this usually happens after few thousands of calls.
The application above exited like this:
...
15706
15707
15708
Process is terminated due to StackOverflowException.
You could learn to see the "Call Stack" windows in the debugger, it will display the list of function calls.
5 Comments
int[] i = new int[999888777]; in c# results in out of memory exception. .. I have edited my post just incase I am wrong.static void fn2() { long x0 = 0 , x1 = 0 , .... , x100100 = 0 , x100101 = 0 ; } produces error: app1.cs(18,840840): error CS0204: Only 65534 locals are allowed , this: static void fn2() { long x0 = 0 , x1 = 0 , .... , x65528 = 0 , x65529 = 0 ; } and this: static void fn2() { long x0 = 0 , x1 = 0 , .... , x65528 = 0 , x65529 = 0 ; Console.WriteLine(x++); fn2(); } Never overflows. ... I think that there are inner checks that C# do to not go into a stackoverflow.