开发者

Why do I get java.lang.OutOfMemoryError given the size of my file?

开发者 https://www.devze.com 2023-02-19 12:39 出处:网络
I\'m parsing a 328kb text file and storing the content in a java List. This is the comma开发者_运维问答nd line (I\'m supposed to use 1GB ram given the parameters).

I'm parsing a 328kb text file and storing the content in a java List.

This is the comma开发者_运维问答nd line (I'm supposed to use 1GB ram given the parameters).

java -Xms128m -Xmx1024m -cp .:jars/* CentroidGenerator data/data.xml

However I get an Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

This is the code, I've commented most of it for debugging: http://d.pr/pRxe thanks


Well, this bit looks wrong to me:

for (int i=0; i<centroids.size(); i++) {            
    Centroid centroid = new Centroid(...);
    // Some other code here
    centroids.add(centroid);
}

So suppose centroids has a size of 1. In the first iteration of the loop, i is 0, which is less than 1. You create a new instance of Centroid, add it to the list, and continue. Now i is 1, but now centroids.size() is 2, so you keep going... etc.

Basically, this loop isn't going to stop until you run out of memory.

It's not clear to me what you're trying to do in the loop, but you don't want to do what you are doing...

0

精彩评论

暂无评论...
验证码 换一张
取 消