Processing and garbage collection ?

edited November 2015 in Using Processing

I'm using a Raspberry Pi 2 with the raspbian jessie image. Processing and the G4P lib and gui builder are working very well but when I close a window by clicking the 'x' the task manager still shows a java thread and a thread with the name of the sketch still running. The java thread is also still consuming some CPU time. So, by using the exit() function what code should I be using to do a complete 'clean up' ??

Answers

  • Couple of questions first.

    1) Are you creating multiple windows using GWindow

    2) The window you are closing the main sketch window (which stops the application) of is it a GWindow object?

  • No multiple windows just the main sketch window.

  • Have you tried it without any G4P controls?

  • Yes, the problem occurs with just a plain sketch without G4P.

  • That means it is not caused by G4P.

    Back in a few minutes :)

  • As far as I remember I had a similar problem running Processing sketches in Eclipse on a Windows machine. It was a long time ago but I remember having the Task Manager open and closing the sketch-application manually.

    I don't know what is causing the problem but you might like to try this sketch out. It will show you the threads that are running at different times in your program.

    This sketch has 2 tabs, the second one is a Java tab NOT a pde tab.

    Try running the sketch and then closing the window, with Task Manager open.

    Main Sketch Tab

    void settings() {
      println("\n###### settings() ########");
      ThreadInfo.printAllThreadInfo();
    }
    
    void setup() {
      size(300, 300);
      println("\n###### setup() ########");
      ThreadInfo.printAllThreadInfo();
    }
    
    void draw() {
    }
    
    void exit() {
      println("\n###### exit() ########");
      ThreadInfo.printAllThreadInfo();
    }
    

    Java tab named
    ThreadInfo.java

    /*
     * Created  25th August 2004
     * Modified for Processing 26th November 2015
     *
     * @author Peter Lager
     */
    public class ThreadInfo {
    
      public static void printAllThreadInfo() {
        ThreadGroup root;
        // find the root of all running threads
        //root = parent = Thread.currentThread().getThreadGroup();
        root = Thread.currentThread().getThreadGroup();
        while (root.getParent() != null) 
          root = root.getParent();
        printThreadGroupInfo("", root);
      }
    
      private static void printThreadGroupInfo(String indent, ThreadGroup group) {
        final int SAFETY = 5;
        if (group == null) return;
    
        System.out.println(indent + "THREAD GROUP: " + group.getName() +
          ": Max Priority: " + group.getMaxPriority() + 
          (group.isDaemon() ? "[Daemon]" : ""));
        // print information about component threads
        int numThreads = group.activeCount();
        Thread threads[] = new Thread[numThreads + SAFETY];
        numThreads = group.enumerate(threads, false);
        for (int i = 0; i < numThreads; i++)
          printThreadInfo(indent + "   ", threads[i]);
        // print infomation about component thread groups
        int numGroups = group.activeGroupCount();
        ThreadGroup groups[] = new ThreadGroup[numGroups + SAFETY];
        numGroups = group.enumerate(groups, false);
        for (int i = 0; i < numGroups; i++)
          printThreadGroupInfo(indent + "   ", groups[i]);
      }
    
      private static void printThreadInfo(String indent, Thread thread) {
        if (thread == null) return;
    
        System.out.println(indent + "THREAD: " + thread.getName() +
          ": Priority: " + thread.getPriority() +
          (thread.isDaemon() ? " [Daemon]" : "") +
          (thread.isAlive() ? " [Alive]" : " [NotAlive]")+
          ((Thread.currentThread() == thread) ? " <== Current" : ""));
      }
    }
    
  • Look for this line in the output

    THREAD: DestroyJavaVM: Priority: 5 [Alive]

  • edited November 2015

    This is the output I got when I ran the program and clicked on the red X

    ###### settings() ########
    THREAD GROUP: system: Max Priority: 10
       THREAD: Reference Handler: Priority: 10 [Daemon] [Alive]
       THREAD: Finalizer: Priority: 8 [Daemon] [Alive]
       THREAD: Signal Dispatcher: Priority: 9 [Daemon] [Alive]
       THREAD: AppKit Thread: Priority: 5 [Daemon] [Alive]
       THREAD: AWT-Shutdown: Priority: 5 [Alive]
       THREAD: Java2D Queue Flusher: Priority: 10 [Daemon] [Alive]
       THREAD: Java2D Disposer: Priority: 10 [Daemon] [Alive]
       THREAD GROUP: main: Max Priority: 10
          THREAD: main: Priority: 5 [Alive] <== Current
    
    ###### setup() ########
    THREAD GROUP: system: Max Priority: 10
       THREAD: Reference Handler: Priority: 10 [Daemon] [Alive]
       THREAD: Finalizer: Priority: 8 [Daemon] [Alive]
       THREAD: Signal Dispatcher: Priority: 9 [Daemon] [Alive]
       THREAD: AppKit Thread: Priority: 5 [Daemon] [Alive]
       THREAD: AWT-Shutdown: Priority: 5 [Alive]
       THREAD: Java2D Queue Flusher: Priority: 10 [Daemon] [Alive]
       THREAD: Java2D Disposer: Priority: 10 [Daemon] [Alive]
       THREAD GROUP: main: Max Priority: 10
          THREAD: Image Fetcher 0: Priority: 8 [Daemon] [Alive]
          THREAD: AWT-EventQueue-0: Priority: 6 [Alive]
          THREAD: Animation Thread: Priority: 5 [Alive] <== Current
          THREAD: DestroyJavaVM: Priority: 5 [Alive]
    
    ###### exit() ########
    THREAD GROUP: system: Max Priority: 10
       THREAD: Reference Handler: Priority: 10 [Daemon] [Alive]
       THREAD: Finalizer: Priority: 8 [Daemon] [Alive]
       THREAD: Signal Dispatcher: Priority: 9 [Daemon] [Alive]
       THREAD: AppKit Thread: Priority: 5 [Daemon] [Alive]
       THREAD: AWT-Shutdown: Priority: 5 [Alive]
       THREAD: Java2D Queue Flusher: Priority: 10 [Daemon] [Alive]
       THREAD: Java2D Disposer: Priority: 10 [Daemon] [Alive]
       THREAD GROUP: main: Max Priority: 10
          THREAD: AWT-EventQueue-0: Priority: 6 [Alive] <== Current
          THREAD: Animation Thread: Priority: 5 [Alive]
          THREAD: DestroyJavaVM: Priority: 5 [Alive]
    
  • Ok, I created the sketch as per your advice and when I close the processing window the task manager still shows the name of the sketch thread and a java thread. The java thread is still using CPU time but the sketch thread is not.

  • Don't forget that the Processing IDE will also appear in Task Manager

  • I have changed the name and category for this discussion because it is not about G4P anymore and you might get more comments that way.

  • Thank you for changing things around. When I run the sketch from within the IDE then close it with the IDE's stop button garbage collection works as it should and the task manager shows a complete clean up.

  • When the sketch is run within the IDE and then closed the task manager still shows two threads one for the sketch and one for the IDE. But when I close the IDE a complete clean up occurs. So this problem is just occuring on the exported 'stand alone' executables. I'm hoping that just a few lines of code within an exit() function can solve the problem.

  • Answer ✓

    Success!....Problem solved by using the code below. This did a complete clean up for an exported executable:

    void exit() { noLoop(); this.dispose(); System.exit(0); }

  • Can someone please change the status of this thread to 'answered'

Sign In or Register to comment.