2

I have a script doing some computation and save some invisible plots into image files inside a for-loop on a linux server.

When I run the script, it usually get stuck in somewhere in the middle. I am not sure where exactly it stopped, but I can know at which iteration of the for-loop it stops by print-out. if I rerun it from the iteration where it stopped, it could continue to run past that place. So it seems to me there is no bug.

I just wonder how I can identify at which line it stops?

what might be the cause of the problem and how I can run the whole script from the beginning till the end?

Thanks!


UPDATE:

I use dbstop

dbstop if error  
dbstop if warning  
run path2script

The running still gets stuck somewhre and no message is given regarding why.

Tim
  • 1
  • 141
  • 372
  • 590
  • You don't have an errant call to PAUSE in there, do you? – gnovice Jan 27 '10 at 02:16
  • No, I don't. Guess probably memory problem, I saw the memory used keeps going up, but there is no message regarding this when it paused. – Tim Jan 28 '10 at 18:34
  • Is it always the same iteration? Define "stuck": Does the script returns to MATLAB prompt? Or hangs, so you have to manually terminate it? Does it produce any error or warning message? I don't think it's memory related, but depends on the situation of course. – yuk Mar 31 '11 at 15:14
  • Hit Ctrl-\ to get the java stack dump and post it. It may help us figure out the problem. – Memming Jun 29 '11 at 16:25

7 Answers7

1

Here is one way to find out what is going on:

  1. Turn on dbstop if error
  2. Once your code is really stuck, hit CTRL+C, this will allow you to inspect the situation and see what is going on

  3. If looking is not enough: select the line where the error was induced and hit f9.

  4. If you still didn't find the error, place a conditional breakpoint that only triggers after you reach the conditons during which/after the error occurs. If this is reached multiple times you are stuck in a loop somehow.

Sidenote: If you are not sure whether you are in a loop, recent versions of Matlab have an autoformatting button, use it!

Dennis Jaheruddin
  • 21,208
  • 8
  • 66
  • 122
0

What type of script is it?

Are you running out of memory? If you're exceeding the maximum allocated heap size, it could crash. When you re-launch it, it starts with all the memory it originally had and can run to completion before it might use all of its memory again.

I'd recommend checking for memory leaks.

Eli
  • 1,269
  • 8
  • 17
  • Thanks! How to check for memory leaks? – Tim Jan 26 '10 at 15:57
  • What's `path2script`? To be honest, I don't know much about MATLAB. You might be able to give yourself more memory by doing something like this: http://www.mathworks.com/support/solutions/en/data/1-18I2C/index.html ... and if you paste some of your script, I can try to see if anything looks terribly inefficient. – Eli Jan 26 '10 at 16:55
0

What exactly happens when your script gets "stuck"? Does it exit back to the prompt, or does matlab simply hang? If it is the latter, then it sounds like you have an infinite loop in your code...

Dima
  • 38,860
  • 14
  • 75
  • 115
0

Difficult to tell without any knowledge about the script and the environment. Are you sure it is stuck and not just busy computing something or getting more memory? You could try to set a conditional breakpoint shortly before the iteration where the hang occurs and then interactively step through the following code using the debugger.

groovingandi
  • 1,986
  • 14
  • 16
0

As groovingandi suggests, set a conditional breakpoint within your code at the start of the iteration where the for loop normally gets stuck. You can do this with a command like:

dbstop in runscript at 500 if iLoop==365 
% where 500 is the first line within the for loop,
% and 365 is the iteration causing problems

If your script gets stuck without breakpoints, but can continue happily past that point if you use breakpoints and then continue, this normally indicates you've got a sporadic failure that's time-dependent, perhaps a race condition. Perhaps you're writing a file to the operating system, and then immediately afterwards looking at the OS to figure out what the next file's name should be, but your filesystem is cacheing slightly? Things like that have caused similar problems for me.

Look carefully at what your code is doing each time round the loop, for anything that might depend on steps before it that might be running asynchronously.

AlexC
  • 3,610
  • 3
  • 26
  • 26
0

You could try using the dbstack function and the save the output, I guess overwritten a file in each iteration would do it.

Juhl
  • 473
  • 4
  • 17
0

I'd like to add that aborting (ctrl+c) a script will throw an error indicating what line it was on at the time of the abort.

Miebster
  • 2,365
  • 4
  • 21
  • 27