Skip to content
Prev 314308 / 398503 Next

R crashing inconsistently within for loops

Sometimes these intermittent "crashes" come from memory misuse, e.g., not allocating enough scratch space.  You can sometimes make those coding errors cause more consistent problems by calling gctorture(TRUE) before running your code.
Here is an example in which it looks like package:gam's lo() is misusing memory when its degree argument is 2 - sometimes it
can do 10 iterations, sometimes 3, sometimes 1:
 
  > library(gam)
  Loading required package: splines
  Loaded gam 1.06.2
  
  >  v <- lapply(1:10,function(i){cat(i,""); gam(mpg ~ lo(hp,degree=2), data=mtcars)})
  1 2 3 4 5 6 7 8 9 10 >
  >  v <- lapply(1:10,function(i){cat(i,""); gam(mpg ~ lo(hp,degree=2), data=mtcars)})
  1 2 3 Error in sys.call() : invalid 'which' argument
  >  v <- lapply(1:10,function(i){cat(i,""); gam(mpg ~ lo(hp,degree=2), data=mtcars)})
  1 Error in sys.call() : invalid 'which' argument 

If I call gctorture(TRUE) before calling lo(degree=2) then it hangs on the first call.  One could attach a debugger at this point to get a clue about where it is failing.

  > library(gam)
  Loading required package: splines
  Loaded gam 1.06.2
  
  >
  > gctorture(TRUE)
  > v <- lapply(1:10,function(i){cat(i,""); gam(mpg ~ lo(hp,degree=2), data=mtcars)})
  1

Using valgrind is helpful also.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com