问题
I'm trying to execute a java script on a large text file, but it gives me this error
java.lang.OutOfMemoryError: Java heap space
I tried to do that on my netbeans project:
Project Properties -> Run -> VM Options -> -Xmx2048m -Xms1024m
I tried also :
System -> Programs -> Java -> Java -> View -> Execution Parameters -> -Xincgc -Xmx2048M
But i didn't solve the problem .. any suggestions please ?
回答1:
String storage can be quite expensive. Using random String of avarage ~ 80 chars long, 3 000 000 strings can cosume almoust 1GB
public static void main(String[] args) {
List<String> list = new ArrayList<>();
String base = RandomStringUtils.random(80);
long i = 0;
try {
while (i < 3e6) {
list.add(base + i++);
}
} finally {
System.out.println("Count:" + list.size());
System.out.println("Memory:" + Runtime.getRuntime().totalMemory() / 1024d / 1024d);
}
}
Output: Count:3000000 Memory:981.5
Try to wrap your piece of code where you are reading your file with the same try-finally
block i have made here. When error will popout, you will get approx ammount of memory you are consuming at that point, and what percentage of file you have managed already to read. This will get you the idea of how much more memory you will need.
Moreover you will know if your -Xmx
directive works, because if it will crash around lets say 500mb it is either ommited by netbeans, or there is no mo available memory it the system.
回答2:
I would suggest altering your code, the other alternative is described in here: http://wiki.netbeans.org/FaqSettingHeapSize
来源:https://stackoverflow.com/questions/39764189/how-to-increase-java-heap-memory-in-netbeans