Results 1 to 7 of 7
Like Tree1Likes

Thread: How to detect infinite loop?

  1. #1
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default 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?

  2. #2
    doWhile is offline Moderator
    Join Date
    Jul 2010
    Location
    California
    Posts
    1,641
    Rep Power
    7

    Default 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

  3. #3
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default 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.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: How to detect infinite loop?

    Then use a logger, as suggested.
    Stick DEBUG logs which you can switch on when needed.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Paul Richards is offline Member
    Join Date
    Oct 2008
    Location
    UK
    Posts
    65
    Rep Power
    0

    Default Re: How to detect infinite loop?

    I've never used a logger with Java... I don't even know what they do. Can anyone suggest a good introductory guide?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: How to detect infinite loop?

    Log4j?
    Not sure what the docs are like, but it's what a lot of places use.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #7
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default 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.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Infinite loop
    By F.S. in forum New To Java
    Replies: 3
    Last Post: 03-09-2012, 01:52 PM
  2. infinite loop
    By javapink in forum New To Java
    Replies: 19
    Last Post: 03-06-2011, 03:28 AM
  3. how to end infinite loop
    By search4survival in forum New To Java
    Replies: 14
    Last Post: 10-25-2010, 09:59 AM
  4. Infinite Loop
    By bosoxfan in forum New To Java
    Replies: 3
    Last Post: 02-22-2010, 02:34 AM
  5. Infinite Loop
    By rclausing in forum New To Java
    Replies: 2
    Last Post: 01-23-2010, 11:11 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •