Marcelo Ruiz
Marcelo Ruiz

Reputation: 393

How to increase stack size to allow more recursion?

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)

Upvotes: 2

Views: 5087

Answers (4)

Kevin Workman
Kevin Workman

Reputation: 42174

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.

Upvotes: 3

Andrew Janke
Andrew Janke

Reputation: 23868

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.

Upvotes: 1

ryekayo
ryekayo

Reputation: 2431

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

Upvotes: 0

Alex
Alex

Reputation: 171

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().

Upvotes: 0

Related Questions