简体   繁体   English

读取一行内容非常长的文件时,发生Java内存不足错误

[英]Java Out of memory error occurs while reading a file having a single line which enormously long

Our application need to read a file with a single line and that single line contains large amount data . 我们的应用程序需要一行读取一个文件,并且该行包含大量数据。 What we are doing is that , read the line from file and store it in string and tokenize the string with - and store to a list . 我们正在做的是,从文件中读取该行并将其存储在字符串中,并使用-标记该字符串并存储到列表中。 From that list some entries are to be checked. 从该列表中将检查一些条目。

the method is as follows 方法如下

public bollean checkMessage(String filename){
boolean retBool = true;
LinkedList tokenList;
int size;
String line = "";
try {
    File file = new File(filename);
    FileInputStream fs = new FileInputStream(file);
    InputStreamReader is = new InputStreamReader(fs);
    BufferedReader br = new BufferedReader(is);
    while ((line = br.readLine()) != null) {
        line.trim();
        tokenList = tokenizeString(line, "-");
        if (tokenList == null) {
            retBool = false;
            resultMsg = "Error in  File.java "                  
        }
        if (retBool) {
                retBool = checkMessagePart(tokenList);
        }
   }
}

the error occurs at line , while ((line = br.readLine()) != null) 错误发生在line,而((line = br.readLine())!= null)

error is 错误是

Caused by: java.lang.OutOfMemoryError: Java heap space
    at java.util.Arrays.copyOf(Arrays.java:2367)
    at java.lang.AbstractStringBuilder.expandCapacity(AbstractStringBuilder.java:130)
    at java.lang.AbstractStringBuilder.ensureCapacityInternal(AbstractStringBuilder.java:114)
    at java.lang.AbstractStringBuilder.append(AbstractStringBuilder.java:535)
    at java.lang.StringBuffer.append(StringBuffer.java:322)
    at java.io.BufferedReader.readLine(BufferedReader.java:363)
    at java.io.BufferedReader.readLine(BufferedReader.java:382)

Actually increasing heapsize didn't work. 实际上增加堆大小是行不通的。 the size of the file trying to read is more than 1gb. 尝试读取的文件大小超过1GB。 Also tried to read as chunks of bytes , but when adding the read data to StringBuilder or list will again generate the MemoryError 也尝试将其读取为字节块,但是将读取的数据添加到StringBuilder或列表时,将再次生成MemoryError

If the problem is that you cannot read the file to a String, then don't do it. 如果问题是您无法将文件读取为字符串,请不要这样做。 Read it token by token by using some other method. 使用其他方法逐个令牌地读取它。 The easy one is using Scanner with the right delimiter ("-" in your case). 简单的方法是使用带有正确分隔符(在您的情况下为“-”)的Scanner If you find its performance lacking, you could resort to implementing your own version of BufferedReader in which the "lines" are split by that character instead of the normal values. 如果发现其性能不足,则可以采用自己的BufferedReader版本,在该版本中,“行”由该字符而不是正常值分隔。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM