!7

// Note: Deadlock solving happens in OS main too, so it will not be
// lost if GC is turned off here.

module GarbageCollection {
  bool on = true;
  
  start { doEvery(1.0, 60.0, r updateMe); }
  
  visualize {
    final JLabel lblTime = jCenteredLabel(" ");
    awtEvery(lblTime, 1000, r {
      long last = lastRegularGC();
      setText(lblTime, last == 0 ? "No GC yet"
        : "Time since last GC: " + iround(elapsedSeconds_timestamp(last)) + " s");
    });
    ret vstackWithSpacing(
      toolTip("On = Perform a Java garbage collection every minute", centerCheckBox(dm_fieldCheckBox("GC On", 'on))),
      lblTime);
  }
  
  void update {
    //print("GC: " + on);
    bool has = hasRegularGC();
    if (has != on) {
      infoBoxAndProgramLog((on ? "Enabling" : "Disabling") + " Garbage Collection");
      if (on) regularGC_firstDelay(1000); else noRegularGC();
    }
  }
}