title: Out of memory exception with timer
body: <p>I have a big problem , I'm using arraylist of object and this object has a timer inside.
   so I'm using say 10 as the timer , so the length of arraylist is 10.</p>
  
  <p>whole timer almost take 80 MB from memory .. and if I used my program for a little time , the program will give me out of memory exception .</p>
  
  <p>because all timer take so much from memory and if I move from one frame to </p>
  
  <p>another frame , the program takes the same amount of memory. </p>
  
  <p>I expected that if I try to cancel all timer or equal the object by null</p>
  
  <p>the memory will be decreased but didn't work.</p>
  
  <p>If the question is not clear please ask in comments. </p>
  
  <p>sorry about my bad English . </p>
  
  <p>note :</p>
  
  <p>this program is simple but the real program is big </p>
  
  <p>my object (edge)  :</p>
  
  <pre><code>package operationresearch.pert.input;
  
  import java.util.ArrayList;
  import java.util.Timer;
  import java.util.TimerTask;
  import javafx.geometry.Insets;
  import javafx.scene.control.Label;
  import javafx.scene.control.TextField;
  import javafx.scene.effect.Bloom;
  import javafx.scene.input.MouseEvent;
  import javafx.scene.layout.HBox;
  
  public class edge extends HBox {
  
      private ArrayList&lt;TextField&gt; predecessor;
      private Timer t2 ;
      private TextField duration, activity;
  
    public edge(boolean haveTime) {
      predecessor = new ArrayList&lt;&gt;();
      activity = new TextField();
      activity.setPrefSize(80, 25);
      activity.setPromptText("Activity");
      if (haveTime) {
          duration = new TextField();
          duration.setPrefSize(85, 25);
          duration.setPromptText("Duration");
      }
      Label depend = new Label("Depend on ");
      depend.setEffect(new Bloom());
      TextField init = new TextField();
      init.setPrefSize(40, 20);
      predecessor.add(init);
  
      this.setSpacing(5);
      if (haveTime) {
          this.getChildren().addAll(activity, duration, depend, init);
      } else {
          this.getChildren().addAll(activity, depend, init);
      }
      setPadding(new Insets(0, 30, 0, 0));
   predecessorControl();
  }
  
  private void predecessorControl() {
       t2= new Timer();
      TimerTask task2 = new TimerTask() {
          @Override
          public void run() {
              TextField last = predecessor.get(predecessor.size() - 1);
              last.setOnMouseClicked((MouseEvent e) -&gt; {
                  if (last == predecessor.get(predecessor.size() - 1)) {
                      TextField newText = new TextField();
                      newText.setPrefSize(50, 20);
                      predecessor.add(newText);
                      getChildren().add(newText);
                  }
                  for (int i = 0; i &lt; predecessor.size(); i++) {
                      System.out.println(predecessor.size());
                      if (predecessor.get(i).getText().equals("") &amp;&amp; i != predecessor.size() - 2 &amp;&amp; i != predecessor.size() - 1) {
                          getChildren().remove(predecessor.get(i));
                          predecessor.remove(predecessor.get(i));
                      }
                  }
              });
          }
      };
      t2.scheduleAtFixedRate(task2, 100, 100);
  }
  
  public ArrayList&lt;TextField&gt; getPredecessor() {
      return predecessor;
  }
  
  public void setPredecessor(ArrayList&lt;TextField&gt; predecessor) {
      this.predecessor = predecessor;
  }
  
  public TextField getDuration() {
      return duration;
  }
  
  public void setDuration(TextField duration) {
      this.duration = duration;
  }
  
  public TextField getActivity() {
      return activity;
  }
  
  public void setActivity(TextField activity) {
      this.activity = activity;
  }
  
  public Timer getT2() {
      return t2;
  }
  
  public void setT2(Timer t2) {
      this.t2 = t2;
  }
  </code></pre>
  
  <p>}</p>
  
  <p>main : </p>
  
  <pre><code>package javaapplication2;
  
  import java.util.ArrayList;
  import javafx.application.Application;
  import javafx.scene.Scene;
  import javafx.scene.layout.Pane;
  import javafx.scene.layout.VBox;
  import javafx.stage.Stage;
  
  
  
      public class TestApp3 extends Application {
  
          private ArrayList&lt;edge&gt; graph;
  
          public static void main(String[] args) {
              launch(args);
          }
  
          @Override 
  
      public void start(Stage stage) throws Exception {
          VBox root = new VBox();
          graph = new ArrayList();
  
          for (int i = 0; i &lt; 10; i++) {
              edge edge = new edge(true);
              graph.add(edge);
              root.getChildren().add(edge);
          }
  
          Scene scene = new Scene(root);
          stage.setScene(scene);
          stage.setWidth(500);
          stage.setHeight(500);
          stage.show();
      }
  }
  </code></pre>
  
  <p><a href="https://i.stack.imgur.com/hasgD.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/hasgD.png" alt="the output "></a></p>