Debugging crazy memory use & crash

I’m seeing bizarre behavior in a relatively new project. Sometimes, when running the game, Unity suddenly and unpredictably becomes unresponsive. Its memory usage (as reported by Windows) is growing rapidly, around 100MB/sec. Mercifully it usually crashes before it swaps my whole machine to disk. Sometimes it is accompanied by a “Fatal error in gc
Too many heap sections” dialog.

Now…I’ve been programming for a long time, long enough to suspect that this is my fault. But I’m having a heck of a time figuring out how one finds things like this in Unity. At work, I’d just pop over to the debugger and “break all” as soon as the behavior starts happening. Here I don’t have that luxury, nor do any of my hastily enabled breakpoints in MonoDevelop seem to get triggered. (I don’t know if they are likely to be enable-able within a frame anyway.)

A bit more context for the curious: Most of what’s running is a simple quadtree representation of passable/impassable areas of my map, and a very simple implementation of A* pathfinding, all in C#. Most of the time the A* works fine. But it seems like, for some combinations of start & goal points, this blow-up happens. I don’t think it’s a garden variety infinite loop within my A* code; certainly that’s possible and I probably did screw it up, but there’s just no way that would blow up like this. For one thing, the open and closed sets actually do use containers with set semantics (e.g. HashSet), and I’m not allocating new nodes, so there’s really no way those could be growing unbounded. I think. In any event, the A* algorithm appears to be working and returning reliable (and obstacle avoiding) results. I don’t even know if it’s related, because I have no way of knowing what’s on the stack when this explosion happens. That’s so frustrating!

So my question is: Unity veterans, how would you go about tracking this down?

1 Answer

1

:slight_smile: That sounds like you have created an infinite loop / recursion in your A* algorithm. If it’s really a simple implementation it shouldn’t be that hard to spot the flaw. If you can’t find it, try to add a Debug.Log() when you trigger the A* routine and print out the parameters. Also add a Debug.Log() at the end when it successfully ended. If Unity crashs, just look into the logfile to see what parameters caused the crash.