How to detect infinite loop?

I have been working on a large (450+ .java files) program for several years, using Netbeans. Today it 'hanged', and I managed to locate the infinite loop, I've put a loop counter on it with an escape, which might not be an ideal solution but its a step forward.

My question is this... is there an easy way to detect which loop is causing the program to 'hang' ?

My approach today has simply been to put System.out.println statements in the code, and when I can see it is stuck I go deeper into the hierarchy of the code and put more statements in. However, this is a time-consuming process as today's fault was VERY deep into a call stack. Its also a bit dangerous since I worry that one day when I delete the System.out.println statements, I will actually remove some good code by mistake.

I have never used the Java Profiler but I wonder if this could help at all?

Re: How to detect infinite loop?

Have you stepped through the code using a debugger? If you add in println's, you can always add in a debug value which can be evaluated - rather than removing all the println's just set the debug value to false (a better way is to have threshold levels, such as those which accompany any descent logging package like that with java.util.logging or Log4j

Re: How to detect infinite loop?

The debugger is a good tool. However, I just out of interest I pasted the output window into a spreadsheet and I found that it was the 224th invocation of this method that hanged. It had been invoked 223 times earlier but on the last time the nature of the data caused the infinite loop. And that's the problem with the debugger... you are pressing continue, continue, continue. continue.... again and again and if you press it too many times you miss the fault.

Re: How to detect infinite loop?

Note that there's also a logging facility present in the SE core library; its functionality is largely stole^H^H^H^H^Hborrowed from the Log4J package. Start reading the API documentation for the Logger class.