如何处理在读取文件时出现“java.lang.OutOfMemoryError: Java heap space”错误?

huangapple go评论54阅读模式
英文:

How to deal with a "java.lang.OutOfMemoryError: Java heap space" error when reading a file?

问题

以下是翻译好的代码部分:

import java.io.*;
import java.util.*;

class MainClass {
    static ArrayList<String> getAttribute(String str) {
        if (str == null) return null;
        ArrayList<String> a = new ArrayList<>();
        int i = 0;
        int j = -1;
        while (i < str.length() && j < str.length()) {
            j = str.indexOf(',', i);
            if (j == -1) j = str.length();
            a.add(str.substring(i, j));
            i = str.indexOf(',', j) + 1;
        }
        return a;
    }

    static ArrayList<ArrayList<String>> readFile(String in) throws IOException {
        ArrayList<ArrayList<String>> a = new ArrayList<>();
        try (FileInputStream fin = new FileInputStream(in);) {
            BufferedReader br = new BufferedReader(new InputStreamReader(fin));
            String str = br.readLine();
            while (str != null) {
                a.add(getAttribute(str));
                str = br.readLine(); //line 26
            }
        }
        return a;
    }

    public static void main(String args[]) throws IOException {
        readFile("stats1.csv"); //line 34
    }
}
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at MainClass.readFile(MainClass.java:26)
at MainClass.main(MainClass.java:34)

这个错误的原因是什么?或者只是因为文件太大?

英文:

I have the following code for reading a file of about 300MB of size and the execution is interrumped by a "java.lang.OutOfMemoryError: Java heap space" exception:

import java.io.*;
import java.util.*;
class MainClass {
static ArrayList&lt;String&gt; getAttribute(String str) {
if(str==null) return null;      
ArrayList&lt;String&gt; a = new ArrayList&lt;&gt;();
int i = 0;
int j = -1;
while(i&lt;str.length() &amp;&amp; j&lt;str.length()) {
j = str.indexOf(&#39;,&#39;,i);
if(j==-1) j = str.length();   
a.add(str.substring(i,j));
i = str.indexOf(&#39;,&#39;,j)+1;
}      
return a;
}
static ArrayList&lt;ArrayList&lt;String&gt;&gt; readFile(String in) throws IOException {
ArrayList&lt;ArrayList&lt;String&gt;&gt; a = new ArrayList&lt;&gt;();
try(FileInputStream fin = new FileInputStream(in);) {         
BufferedReader br = new BufferedReader(new InputStreamReader(fin));
String str = br.readLine();
while(str!=null) {
a.add(getAttribute(str));
str = br.readLine(); //line 26
}
}
return a;
}   
public static void main(String args[]) throws IOException
{      
readFile(&quot;stats1.csv&quot;); //line 34
}
}
Exception in thread &quot;main&quot; java.lang.OutOfMemoryError: Java heap space
at MainClass.readFile(MainClass.java:26)
at MainClass.main(MainClass.java:34)

What is the cause of this error? or is it just that the file is too big?

答案1

得分: 2

查看ArrayList类的默认构造函数的源代码,该构造函数不接受任何参数。它创建了一个大小为十的数组。然后查看ArrayList类中add()方法的源代码。一旦添加了十个元素,数组就需要扩展。它的大小增加了一半。换句话说,当您添加第十一个元素时,数组的大小增加到15。当您添加第十六个元素时,数组的大小增加到22,依此类推。一旦达到较大的数字,添加单个元素将导致数组大小的巨大增加。因此,建议使用接受初始容量参数的ArrayList构造函数。因此,您可以计算文件中的行数,以获取ArrayList所需容量的想法。有关一种计算文件中行数的方法,请参阅Stack Overflow问题Java NIO Files count() 方法用于计算行数

英文:

Look at the source code for the default constructor, that takes no arguments, of class ArrayList. It creates an array of size ten. Then look at the source code for method add() in class ArrayList. Once ten elements have been added, the array needs to be increased. It is increased by half. In other words, when you add the eleventh element, the array is increased to size 15. When you add the sixteenth element, the array is increased to 22 and so on. Once you get to big numbers, adding a single element will cause a huge increase in the array size. Hence it is recommended to use the ArrayList constructor that accepts an initial capacity argument. So maybe you could count the number of lines in the file to get an idea of what capacity your ArrayList requires. Refer to the SO question Java NIO Files count() Method for Counting the Number of Lines for one way to count the number of lines in a file.

huangapple
  • 本文由 发表于 2020年9月30日 00:50:35
  • 转载请务必保留本文链接:https://go.coder-hub.com/64124166.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定