title: Exception in thread "main" java.lang.OutOfMemoryError: Java heap space while using util Packages
body: <p>My problem statement:</p>
  
  <p>There will be given set of words in a file (>5000 words). We need return a list of anagrams separated by comma(,) in each string(set of anagrams)
  Eg: [alter,later, part,trap, elbow,below, listen,silent , tensil ]</p>
  
  <p>Exception:</p>
  
  <pre><code>Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
  at java.util.Arrays.copyOf(Arrays.java:3210)
  at java.util.Arrays.copyOf(Arrays.java:3181)
  at java.util.ArrayList.grow(ArrayList.java:265)
  at java.util.ArrayList.ensureExplicitCapacity(ArrayList.java:239)
  at java.util.ArrayList.ensureCapacityInternal(ArrayList.java:231)
  at java.util.ArrayList.add(ArrayList.java:462)
  at FindAnagrams01.anagramsList(FindAnagrams01.java:25)
  at FindAnagrams01.main(FindAnagrams01.java:7)
  </code></pre>
  
  <p>My code is :</p>
  
  <pre><code>  public static List&lt;String&gt; anagramsList(String filePath) throws IOException {
      BufferedReader br = new BufferedReader(new FileReader(filePath));
      String str = br.readLine();
      List&lt;List&lt;String&gt;&gt; result = new ArrayList&lt;List&lt;String&gt;&gt;(10000);
      HashMap&lt;String, ArrayList&lt;String&gt;&gt; map = new HashMap&lt;String,ArrayList&lt;String&gt;&gt;(10000);
      while(str != null) {
          char[] arr = new char[26];
          for(int i = 0; i &lt; str.length(); i++) {
              arr[str.charAt(i) - 'a']++;              
          }
          String ns = new String(arr);
          if(map.containsKey(ns)){
              map.get(ns).add(str);
          } else {
              ArrayList&lt;String&gt; al = new ArrayList&lt;String&gt;(10000);
              al.add(str);
              map.put(ns, al);
          }
      }
      br.close();
      result.addAll(map.values());
      String res[] = new String[10000];
      for(int i = 0; i &lt; result.size(); i++) {
          int isIntial = 0;
          for(String j : result.get(i)) {
              if ((result.get(i).size()) &gt; 1) {
                  if (isIntial == 0) {
                      res[i] = j;
                      isIntial = 1;
                  }
                  else
                      res[i] += "," + j;
              } 
          }
      }
      List&lt;String&gt; angrms = new ArrayList&lt;String&gt;(10000);
      for (int i = 0; i &lt; res.length; i++) {
          if (res[i] != null)
              angrms.add(res[i]);
      }
      return angrms;
  }
  </code></pre>