- Home /
Too subjective and argumentative
Too many nested loops cause fatal error(?)
I have 2 nested loops, each has 9 or 10 foreach loops and if statements nested. When I try to play in editor, I get an error message that says "Fatal error! Could not allocate memory : System out of memory". I'm developing for mobile. What I have in my scene is just a few spheres and blocks. Nothing fancy to crash Unity. I think it's that nested loops that cause the problem. Do you know what may cause fatal error or did you have trouble using bunch of nested loops in Unity before?
Nesting loops won't cause a system out of memory error; it's what you do in those loops.
for(int i=0;i<10;i++){
for(int j=0; j<10; j++){
for(int k=0; k<10; k++){
//This block is run 1 000 times.
}
}
}
Now, if this is in your Update, you will have run the innermost block ~60 000 times (assu$$anonymous$$g 60 FPS) in a second. Even allocating a new string in that loop could cause a system out of memory error.
I don't use them in my update function. I get one time ride then delete unnecessary variable values.
What are you doing in the loop? It's hard to solve your problem without knowing what might be allocating memory.
To echo Jamora, you're probably miscounting the # of times the loops run, and really are allocating too much (9 or 10? So, you have more loops than you can count?)
Or, another commonish problem, a foreach is going through a list and growing that list at the same time, so runs forever.
No, nothing like that and it's not that big deal. $$anonymous$$y list elements aren't more than 10. I guess it's an unnecessary way to use that much memory. I'll find a better way. Thank you all.
Follow this Question
Related Questions
gc: too many threads, crash using Unity 4.2 8 Answers
Fatal Error While Playing -- VertexData.cpp 0 Answers
crash on android devices (unity 4.6f1) 2 Answers
Unity crashes when opening a project. 1 Answer
FATAL EXCEPTION [UnityMain] 0 Answers