Note that there are some explanatory texts on larger screens.

plurals
  1. POjava.lang.OutOfMemoryError even though plenty of
    text
    copied!<p>I am trying to read a 2.5GB txt file into my application. I am running Win7 x64 and have 43GB of mem available (out of 64GB). I tried playing around with -Xmx -XX:MaxParmSize -XX:ParmSize etc. None of these affect the error. What else could I try? This error seems very odd as I certainly have enough heap space available.</p> <pre><code>Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit at java.util.Arrays.copyOf(Unknown Source) at java.lang.AbstractStringBuilder.expandCapacity(Unknown Source) at java.lang.AbstractStringBuilder.ensureCapacityInternal(Unknown Source) at java.lang.AbstractStringBuilder.append(Unknown Source) at java.lang.StringBuilder.append(Unknown Source) at j.utilities.IO.loadString(IO.java:187) at j.utilities.IO.loadString(IO.java:169) at city.PreProcess.main(PreProcess.java:78) </code></pre> <p>I am running</p> <pre><code>java version "1.7.0_09" Java(TM) SE Runtime Environment (build 1.7.0_09-b05) Java HotSpot(TM) 64-Bit Server VM (build 23.5-b02, mixed mode) </code></pre> <p>Thanks a lot in advance.</p> <p>============== ANSWER ==============</p> <p>OK, I just tested it with</p> <pre><code>StringBuilder sb = new StringBuilder(); for ( int i=1; i&lt;Integer.MAX_VALUE; i++ ) sb.append("x"); </code></pre> <p>and got</p> <pre><code>Exception in thread "main" java.lang.OutOfMemoryError: Requested array size exceeds VM limit at java.util.Arrays.copyOf(Unknown Source) ... </code></pre> <p>Thus, it really is StringBuilder which tries to build an array bigger than Integer.MAX_VALUE.</p> <p>In case interested</p> <pre><code>StringBuilder sb = new StringBuilder(); int i=1; try { for ( ; i&lt;Integer.MAX_VALUE; i++ ) sb.append("x"); } catch ( OutOfMemoryError e ) { System.out.println(i); // OUTPUT: 1207959551 System.out.println(Integer.MAX_VALUE); // OUTPUT: 2147483647 } </code></pre> <p>With StringBuilder you can accumulate 1,207,959,550 chars - far less than Integer.MAX_VALUE.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload