Why does Unity crash? (and other programs in general)

Hey people!

Do you know why Unity eventually crashes when I am processing my data through massive recursions in a separate thread?
I am passing a function into a Thread object and Start it.

The thing is, those recursions are really massive, up to 7 million cycles in about 2 minutes with my cpu. I have 8 GB ram core i7.

question:

What’s the cause of a program’s crash after 3 minutes? If I reduce the cycles, and the recursion depth, it executes just fine. Does it always happen when program runs out of memory?
If so, is there a way to increase it’s memory, to give it more space, using c#?

Thank you very much!

Well, it seems mono has a hard limit(~4MB) on the stacksize which can’t be increased without changing the monoruntime itself. Since Unity uses it’s own build of mono you can’t really change it. It’s hard to say when your program runs out of stack. It depends entirely on your recursion depth and on the stackframe size of one recursion. It might also depend on how your recursion is implemented. If done the right way the compiler might optimise the recursive calls into tail calls which wouldn’t require any additional memory. However i don’t know if the C# compiler that Unity uses does such optimisations.