How detect infinite loop in Java?

4.9k views Asked by At

I run a Java program, and it doesn't ends in a long time. I guess it may be a infinite loop in the code, but I can't see the code(can't modify it).

How to judge it's in a infinite loop or not?


It's an interview question and my answer is judge PC(program counter) is in loop or not. But the interviewer give me the hint use stack and heap of that program...

1

There are 1 answers

3
twentylemon On BEST ANSWER

In theory, you can't. That's the halting problem.

In practice, you can check for cycles in the call stack. Java should crash once it runs out of memory and dump a stack trace. Tedious, maybe potentially useful.