title: java collection Out of memory error
body: <p>Im trying to time the Arraylist and Linkedlist by populating more than million rows and get the below error after the Arraylist population,</p>
  
  <p>Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
      at java.lang.Integer.valueOf(Integer.java:642)
      at scratch.Collectionss.main(Collectionss.java:25)</p>
  
  <p>How do I avoid this error, I tried setting the l1 = null but that gives me a error,</p>
  
  <pre><code>public class Collectionss {
      public static void main(String[] args){
          // 
          long starttime = System.currentTimeMillis();
          List&lt;Integer&gt; l1 = new ArrayList&lt;Integer&gt;();
          for (int i = 1; i &lt;= 10000000; i++){
              l1.add(i);
          }
          System.out.println(l1.size());
  
          long endtime = System.currentTimeMillis();
  
          System.out.println(endtime - starttime);
  
          //
          long starttime1 = System.currentTimeMillis();
          List&lt;Integer&gt; l2 = new LinkedList&lt;Integer&gt;();
          for (int i = 1; i &lt;= 10000000; i++){
              l2.add(i);
          }
          System.out.println(l2.size());
  
          long endtime1 = System.currentTimeMillis();
  
          System.out.println(endtime1 - starttime1);
  
      }
  
  }
  </code></pre>