title: Creating new objects inside ArrayList's add() method is causing memory leaks. What can I do differently?
body: <p>I am working on a minesweeper-like game in Java and was noticing memory leaks occurring whenever I reset the board.  I did a test below and found out that whenever I created a new object inside an ArrayList's add() method, the garbage collector would not dump the elements from the memory whenever I used the clear() method.  Here's some code for reference:</p>
  
  <pre><code>import java.util.ArrayList;
  import java.util.Scanner;
  
  public class MemLeakTest {
  
      public static void main(String[] args) {
          Scanner input = new Scanner(System.in);
          ArrayList&lt;PointlessObject&gt; list = new ArrayList();
  
          String selection = "1";
  
          while(selection.equals("1")){
              System.out.print("Type 1 to run test:");
              selection = input.next();
              System.out.println();
  
              //begin test
              if(selection.equals("1")){
                  for(int i = 0; i&lt;100000; i++){
                      list.add(new PointlessObject());
                  }
              }
              list.clear(); // ArrayList elements remained in memory
          }
      }
  
  }
  
  class PointlessObject{
      String text;
  
      public PointlessObject(){
          text = "Stuff";
      }
  }
  </code></pre>
  
  <p>What would be a better alternative to creating a long list of objects in which I can minimize memory leaks?</p>