title: java.lang.OutOfMemoryError: Java heap space error in my code
body: <p>I am trying to read a JSON File and do some operations. This JSON file is in <code>Amazon S3</code> and it is 500MB in size. Future files will be in TeraBytes. Below is my code.</p>
  
  <pre><code>public void createHash() throws IOException
      {
          System.out.println("Hash Creation Started");
  
          strBuffer = new StringBuffer("");
  
  
          try
          {
              //List all the Buckets
              List&lt;Bucket&gt;buckets = s3.listBuckets();
  
              for(int i=0;i&lt;buckets.size();i++)
              {
                  System.out.println("- "+(buckets.get(i)).getName());
              }
  
  
              //Downloading the Object
              System.out.println("Downloading Object");
              S3Object s3Object = s3.getObject(new GetObjectRequest(inputBucket, inputFile));
              System.out.println("Content-Type: "  + s3Object.getObjectMetadata().getContentType());
  
  
              //Read the JSON File
              /*BufferedReader reader = new BufferedReader(new InputStreamReader(s3Object.getObjectContent()));
              while (true) {
                  String line = reader.readLine();
                  if (line == null) break;
  
                 // System.out.println("    " + line);
                  strBuffer.append(line);
  
              }*/
  
              JSONTokener jTokener = new JSONTokener(new BufferedReader(new InputStreamReader(s3Object.getObjectContent())));
              jsonArray = new JSONArray(jTokener);
  
              System.out.println("Json array length: "+jsonArray.length());
  
  
              for(int i=0;i&lt;jsonArray.length();i++)
              {
                  JSONObject jsonObject1 = jsonArray.getJSONObject(i);
                  StringBuffer hashIndex = new StringBuffer("");
  
                  //Add Title and Body Together to the list
                  String titleAndBodyContainer = jsonObject1.getString("title")+" "+jsonObject1.getString("body");
  
  
                  //Remove full stops and commas
                  titleAndBodyContainer = titleAndBodyContainer.replaceAll("\\.(?=\\s|$)", " ");
                  titleAndBodyContainer = titleAndBodyContainer.replaceAll(",", " ");
                  titleAndBodyContainer = titleAndBodyContainer.toLowerCase();
  
  
                  //Create a word list without duplicated words
                  StringBuilder result = new StringBuilder();
  
                  HashSet&lt;String&gt; set = new HashSet&lt;String&gt;();
                  for(String s : titleAndBodyContainer.split(" ")) {
                      if (!set.contains(s)) {
                          result.append(s);
                          result.append(" ");
                          set.add(s);
                      }
                  }
                  //System.out.println(result.toString());
  
  
                  //Re-Arranging everything into Alphabetic Order
                  String testString = "acarpous barnyard gleet diabolize acarus creosol eaten gleet absorbance";
                  //String testHash = "057        1$k     983    5*1      058     52j    6!v   983     03z";
  
                  String[]finalWordHolder = (result.toString()).split(" ");
                  Arrays.sort(finalWordHolder);
  
  
                  //Navigate through text and create the Hash
                  for(int arrayCount=0;arrayCount&lt;finalWordHolder.length;arrayCount++)
                  {
  
  
                      if(wordMap.containsKey(finalWordHolder[arrayCount]))
                      {
                          hashIndex.append((String)wordMap.get(finalWordHolder[arrayCount]));
                      }
  
                  }
  
                  //System.out.println(hashIndex.toString().trim());
  
                  jsonObject1.put("hash_index", hashIndex.toString().trim()); //Add the Hash to the JSON Object
                  jsonObject1.put("primary_key", i); //Create the primary key
                  jsonObjectHolder.add(jsonObject1); //Add the JSON Object to the JSON collection
  
                  jsonHashHolder.add(hashIndex.toString().trim());
  
                  System.out.println("JSON Number: "+i);
  
                  //System.out.println(Arrays.toString(finalWordHolder));
                  //System.out.println("- "+hashIndex.toString());
  
                  //break;
  
              }
  
              System.out.println("Hash Creation Completed");
          }
          catch(Exception e)
          {
              e.printStackTrace();
          }
      }
  </code></pre>
  
  <p>But, I get the following error when the code is executed.</p>
  
  <pre><code>Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
          at java.util.Arrays.copyOf(Arrays.java:2894)
          at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:117)
          at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:589)
          at java.lang.StringBuffer.append(StringBuffer.java:337)
          at com.amazonaws.util.json.JSONTokener.nextString(JSONTokener.java:284)
          at com.amazonaws.util.json.JSONTokener.nextValue(JSONTokener.java:348)
          at com.amazonaws.util.json.JSONObject.&lt;init&gt;(JSONObject.java:222)
          at com.amazonaws.util.json.JSONTokener.nextValue(JSONTokener.java:351)
          at com.amazonaws.util.json.JSONArray.&lt;init&gt;(JSONArray.java:125)
          at HashCreator.createHash(HashCreator.java:215)
          at HashCreator.&lt;init&gt;(HashCreator.java:61)
          at Main.main(Main.java:9)
  [root@ip-172-31-45-123 JarFiles]#
  </code></pre>
  
  <p>It talks about line number 215, and it is this code - <code>jsonArray = new JSONArray(jTokener);</code></p>
  
  <p>What is happening here and how can I solve this?</p>
  
  <p><strong>Edit</strong></p>
  
  <p>This is my JSON code:</p>
  
  <pre><code>[
      {
          "hash_index": "00102x05h06l0aj0dw",
          "body": "Who's signing up for Obamacare?",
          "_type": "ArticleItem",
          "title": "Who's signing up for Obamacare? - Jan. 13, 2014",
          "source": "money.cnn.com",
          "primary_key": 0,
          "last_crawl_date": "2014-01-14",
          "url": "http://money.cnn.com/2014/01/13/news/economy/obamacare-enrollment/index.html"
      },
      {
          "hash_index": "00102x05h06l0aj0dw0iz0kn0l@0t#0",
          "body": "Who's signing up for Obamacare?",
          "_type": "ArticleItem",
          "title": "Who's signing up for Obamacare? - Jan. 13, 2014",
          "source": "money.cnn.com",
          "primary_key": 1,
          "last_crawl_date": "2014-01-14",
          "url": "http://money.cnn.com/2014/01/13/news/economy/obamacare-enrollment/index.html"
      }
  ]
  </code></pre>
  
  <p>Anyway, please don't provide me the solution "increase your heap size". It is not a solution, it is a "plaster".</p>