What exception do we get with low RAM in memory?

If I execute a recursive algorithm to traverse a tree ... and I know that it has a lot of data, huge .. and after 20 minutes I get a "Stack Overflow" exception. But StackOverFlow may be due to some error in the code, possibly due to an infinite loop ... so I'm just trying to make sure that this SOF error that I get in VisualStudio is related to the fact that my recursive algorithm runs out of RAM, and not because of other errors ... Do we have a special type of error message or an exception when we run out of memory in a recursive algorithm?

0
source share
6 answers

System.OutOfMemoryException can be thrown when memory is low. System.StackOverflowException is that you have confused the call stack with something recursively supercomplex or without a ending case.

+1
source

If you run out of memory, you will get an OutOfMemory exception

+3
source

System.OutOfMemoryException

http://msdn.microsoft.com/en-us/library/system.outofmemoryexcetion.aspx

Just as @SLaks pointed out that a StackoverflowException never occurs due to out of memory.

+2
source

The closest thing you need to report about "low RAM" is an OutOfMemoryException , but this only happens when it is not possible to allocate enough continuous RAM for the next operation. This does not mean that the host is low on RAM.

A StackOverflowException may contain this as an internal exception, but I'm not sure.

+1
source

If your tree is deep, why not use your own stack instead of using recursion? Sort of:

 Stack<Node> stack = new Stack<Node>(); stack.Push(rootNode); Node currentNode; while( (currentNode = stack.Pop()) != null) { foreach(var childNode in currentNode.Children) { stack.Push(childNode); } //process this node. } 
+1
source

I did a little recursive experiment that gave me about 87,000 iterations before the stack space ended. A method call always uses the stack, not the heap. If there is a way to create a stack-based heap, then you can leave a little more. In this regard, read the following article (although it cannot be used in C #!):

recursion using only heap area

Also check this out ...

http://joel.inpointform.net/software-development/explanation-of-stack-heap-and-recursion-causing-stack-overflow/

Change Answering your question ...

Typically, the fact is that if your application tries to exceed the stack space, you will get a StackOverflowException. If your application tries to exceed a bunch of space, you will get an OutOfMemoryException

+1
source

Source: https://habr.com/ru/post/1441244/


All Articles