1

I'm running a recursion method in processing, but when the job is too big, it give me this error:

crashed in event thread due to Timeout occurred while waiting for packet 139.

But it works when the recursion is small. Is there any way to increase the stack for bigger recursion problems?

This is my code is for painting figures on the screen. It works for small figures, but not for bigger ones.

boolean pit;
int xc;
int yc;
color negro;
color rojo;
color c;
long tiempoI;
long tiempoF;
long espera;
void setup(){
  size(500,500);
  negro=color(0,0,0);
  negro=color(0,0,0);
  rojo=#FF0000;
  pit=false;
  tiempoI=millis();
  tiempoF=millis();
  espera=5;
}
void draw(){
  background(240);
  noSmooth();
  //dibujarRectangulo(0,0,300,300);
  rect(0,0,100,100);
  if(pit){
    pintar(xc,yc);
  }
}
void mousePressed() {
  xc=mouseX;
  yc=mouseY;
  pit=true;
  loadPixels();
  c=pixels[xc+(width*yc)];
  println(red(c)+" "+green(c)+" "+blue(c));


}
public void pintar(int x,int y){
  if(x<width&&x>0&&y<height&&y>0){
    stroke(rojo);
    c=get(x,y);
    if(c!=rojo&&c!=negro){
      point(x,y);
    }
    c=get(x+1,y);
    if(c!=rojo&&c!=negro){
      pintar(x+1,y);
    }

    c=get(x-1,y);
    if(c!=rojo&&c!=negro){
      pintar(x-1,y);
    }

    c=get(x,y+1);
    if(c!=rojo&&c!=negro){
      pintar(x,y+1);
    }

    c=get(x,y-1);
    if(c!=rojo&&c!=negro  ){
      pintar(x,y-1);
    }

  }
}

The traceback:

crashed in event thread due to Timeout occurred while waiting for packet 139. org.eclipse.jdi.TimeoutException: Timeout occurred while waiting for packet 139. at org.eclipse.jdi.internal.connect.PacketReceiveManager.getReply(PacketReceiveManager.java:186) at org.eclipse.jdi.internal.connect.PacketReceiveManager.getReply(PacketReceiveManager.java:197) at org.eclipse.jdi.internal.MirrorImpl.requestVM(MirrorImpl.java:191) at org.eclipse.jdi.internal.MirrorImpl.requestVM(MirrorImpl.java:226) at org.eclipse.jdi.internal.ThreadReferenceImpl.frames(ThreadReferenceImpl.java:257) at org.eclipse.jdi.internal.ThreadReferenceImpl.frames(ThreadReferenceImpl.java:240) at processing.mode.java.runner.Runner.findException(Runner.java:888) at processing.mode.java.runner.Runner.reportException(Runner.java:871) at processing.mode.java.runner.Runner.exceptionEvent(Runner.java:797) at processing.mode.java.runner.Runner$2.run(Runner.java:688) org.eclipse.jdi.TimeoutException: Timeout occurred while waiting for packet 140. at org.eclipse.jdi.internal.connect.PacketReceiveManager.getReply(PacketReceiveManager.java:186) at org.eclipse.jdi.internal.connect.PacketReceiveManager.getReply(PacketReceiveManager.java:197) at org.eclipse.jdi.internal.MirrorImpl.requestVM(MirrorImpl.java:191) at org.eclipse.jdi.internal.MirrorImpl.requestVM(MirrorImpl.java:226) at org.eclipse.jdi.internal.VirtualMachineImpl.exit(VirtualMachineImpl.java:716) at processing.mode.java.runner.Runner.close(Runner.java:961) at processing.mode.java.JavaEditor.handleStop(JavaEditor.java:728) at processing.mode.java.JavaToolbar.handlePressed(JavaToolbar.java:96) at processing.app.EditorToolbar.mousePressed(EditorToolbar.java:474) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) org.eclipse.jdi.TimeoutException: Timeout occurred while waiting for packet 141. at org.eclipse.jdi.internal.connect.PacketReceiveManager.getReply(PacketReceiveManager.java:186) at org.eclipse.jdi.internal.connect.PacketReceiveManager.getReply(PacketReceiveManager.java:197) at org.eclipse.jdi.internal.MirrorImpl.requestVM(MirrorImpl.java:191) at org.eclipse.jdi.internal.MirrorImpl.requestVM(MirrorImpl.java:226) at org.eclipse.jdi.internal.VirtualMachineImpl.exit(VirtualMachineImpl.java:716) at processing.mode.java.runner.Runner.close(Runner.java:961) at processing.mode.java.JavaEditor.handleStop(JavaEditor.java:728) at processing.mode.java.JavaToolbar.handlePressed(JavaToolbar.java:96) at processing.app.EditorToolbar.mousePressed(EditorToolbar.java:474) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source) org.eclipse.jdi.TimeoutException: Timeout occurred while waiting for packet 142. at org.eclipse.jdi.internal.connect.PacketReceiveManager.getReply(PacketReceiveManager.java:186) at org.eclipse.jdi.internal.connect.PacketReceiveManager.getReply(PacketReceiveManager.java:197) at org.eclipse.jdi.internal.MirrorImpl.requestVM(MirrorImpl.java:191) at org.eclipse.jdi.internal.MirrorImpl.requestVM(MirrorImpl.java:226) at org.eclipse.jdi.internal.VirtualMachineImpl.exit(VirtualMachineImpl.java:716) at processing.mode.java.runner.Runner.close(Runner.java:961) at processing.mode.java.JavaEditor.handleStop(JavaEditor.java:728) at processing.mode.java.JavaToolbar.handlePressed(JavaToolbar.java:96) at processing.app.EditorToolbar.mousePressed(EditorToolbar.java:474) at java.awt.Component.processMouseEvent(Unknown Source) at javax.swing.JComponent.processMouseEvent(Unknown Source) at java.awt.Component.processEvent(Unknown Source) at java.awt.Container.processEvent(Unknown Source) at java.awt.Component.dispatchEventImpl(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.LightweightDispatcher.retargetMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.processMouseEvent(Unknown Source) at java.awt.LightweightDispatcher.dispatchEvent(Unknown Source) at java.awt.Container.dispatchEventImpl(Unknown Source) at java.awt.Window.dispatchEventImpl(Unknown Source) at java.awt.Component.dispatchEvent(Unknown Source) at java.awt.EventQueue.dispatchEventImpl(Unknown Source) at java.awt.EventQueue.access$200(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.awt.EventQueue$3.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.awt.EventQueue$4.run(Unknown Source) at java.security.AccessController.doPrivileged(Native Method) at java.security.ProtectionDomain$1.doIntersectionPrivilege(Unknown Source) at java.awt.EventQueue.dispatchEvent(Unknown Source) at java.awt.EventDispatchThread.pumpOneEventForFilters(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForFilter(Unknown Source) at java.awt.EventDispatchThread.pumpEventsForHierarchy(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.pumpEvents(Unknown Source) at java.awt.EventDispatchThread.run(Unknown Source)

Marcelo Ruiz
  • 373
  • 3
  • 14

4 Answers4

3

Your error is indeed a StackOverflow caused by too much recursion, but Processing covers that up with the strange error you're seeing. Documentation on that bug is here.

You can increase the Java stack size to increase the limit of recursive calls. Info can be found here, but the gist is that you have to pass the -Xss setting into Java at runtime.

However, that setting requires that you run your sketch as a Java application. This is possible, but it involves exporting your sketch as a jar, then running the jar via the command prompt, or switching to eclipse. That's a lot more work than simply hitting the run button in Processing- and any users you send your jar would have to do the same.

Instead, you should probably refactor your algorithm to eliminate the excessive recursion.

Community
  • 1
  • 1
Kevin Workman
  • 41,537
  • 9
  • 68
  • 107
  • It works exporting the code to Eclipse and adding the arguments in the VM arguments as it said here http://stackoverflow.com/questions/2127217/java-stack-overflow-error-how-to-increase-the-stack-size-in-eclipse – Marcelo Ruiz May 04 '15 at 22:41
  • @Pulse9 Cool. Keep in mind that if you want to deploy as a jar, other people will also have to pass in the VM arguments. Shameless self-promotion: you can use something like [JarMatey](http://staticvoidgames.com/JarMatey/) to do that automatically. – Kevin Workman May 04 '15 at 22:47
  • 1
    Take a look at the `%APPDATA%\Processing\preferences.txt` file (which appears after you've run Processing). That's its per-user preferences, in a Java properties format file. The `run.options` pref appears to be for extra `java` command line arguments, to which you could add `-Xss` to adjust your Processing behavior without affecting other users or programs. See the `lib/defaults.txt` file in the Processing installation for comments documenting the various preferences. – Andrew Janke May 04 '15 at 23:05
  • (Rewriting your algorithm to eliminate the excess recursion is still a better approach than adjusting the stack size, though.) – Andrew Janke May 04 '15 at 23:07
1

It might just be slow. Going from a 100 x 100 image to a 500 x 500 image increases the number of pixels from 10,000 to 250,000. That's a big jump. And your recursive method is going to end up making more than one call per pixel. That could bog things down.

To see if that's the case, try gradually increasing your image size and timing how long your program takes as the image size increases. You can expect the 500x500 version to take 25x as long as the 100x100 version. Is that a reasonable execution time for you?

If it's a performance problem like that, see if you can switch this to an iterative implementation, and maybe hoist that stroke() call out of the loop.

Also be aware that draw() is called inside a loop, unless you control it with noLoop() and redraw(). Which you might want to do here. Your draw() might be slow enough on larger images that it can't run in its allotted timeslice, and the draw() calls and event handling are getting backed up.

Andrew Janke
  • 23,508
  • 5
  • 56
  • 85
0

It is possible that you are reaching the maximum amount of recursive calls before getting a stackoverflow exception. I would recommend changing your heap space size in the Java Control Panel.

  1. Go to the Control Panel in Windows.
  2. Click on Java.
  3. Go to the Java tab and click on View.
  4. You will see a section called Runtime Parameters. Double click on that column (make sure you double click on the version you are currently using). This will allow you to edit it.
  5. There are a few ways to set your heap size. I will list them:

      -Xms<size>        set initial Java heap size
    
      -Xmx<size>        set maximum Java heap size
    
      -Xss<size>        set java thread stack size
    
  6. As an example, I have set mine to: -Xms512m. Don't forget to Apply/Save the changes made.

For Eclipse, you can also set the heap space:

  1. In Run->Run Configuration find the Name of the class you have been running, select it, click the Arguments tab then add: -Xms512M -Xmx1524M to the VM Arguments section
ryekayo
  • 2,341
  • 3
  • 23
  • 51
  • Try setting it in eclipse if you havent – ryekayo May 04 '15 at 21:54
  • 1
    Ooh, don't do this! Changing the settings in the Java control panel could affect all the Java programs on your machine that don't have explicit settings for these. And setting `-Xss515` might break a bunch of them; that makes your stack 515 bytes (not KB or MB), which is very small. – Andrew Janke May 04 '15 at 21:58
  • Oh i missed that! Yeah you do NOT wanna set it to 515 bytes lol. If you are going to change it , make sure it is a considerable amount of space – ryekayo May 04 '15 at 22:00
  • oh sorry mean -Xmx515m -Xss515m m for both – Marcelo Ruiz May 04 '15 at 22:05
  • Now `-Xss515m` would giving all your Java programs a half gigabyte stack per thread. That is a *huge* stack and might well cause performance problems on your machine or break other Java programs. Don't do that either. – Andrew Janke May 04 '15 at 22:14
0

It is possible to exceed stack on recursion. Your tree should be not too deep. See some consideration here.

However, base on your stacktrace and code you had provide issue is not in this part of the code. It looks, based on stacktrace, that you recursively invoke some GUI methods or Mouse Event handling methods not the method pintar().

Community
  • 1
  • 1
Alex
  • 171
  • 1
  • 8
  • I added the full code, the problem should be the recursive method, it works when paint a rect 100x100 but not bigger – Marcelo Ruiz May 04 '15 at 21:51
  • It's not full code so it's hard to reproduce the error. However, what I can see from your code, it's not recursion - it's infinite calling pintar() itself. Usually recursion should be interrupted by something - depth or reaching some goal - it should be prevented from endless call. I do not see such limitation in your code. With some values x and y it can run forever and so reach end of the available memory for sure. – Alex May 04 '15 at 22:18
  • Yes it is the full code, you can copy paste and run it in processing. And also I have a stop condition, that why there is a lot of IFs to call the function itself. It works perfectly on the small square I paint :) – Marcelo Ruiz May 04 '15 at 22:22