BufferedReader字符串内存泄漏?

Jac*_*son 4 java memory string bufferedreader

我对AP计算机科学的一个项目工作,我们基本上是做一个图书馆,在那里你可以添加客户和书籍,借书给客户,等等,我们必须能够读取/写入CSV.我在使用这段代码时遇到了一些麻烦(这只是其中的一部分,而且我很确定其余部分是有效的,整个过程总共接近600行):

public void readForBook()  {    
    try{ 
        BufferedReader CSVFile = new BufferedReader(new FileReader("book.csv"));

        String dataRow = CSVFile.readLine();
        dataRow = CSVFile.readLine();
        while (dataRow != null){
            String[] dataArray = dataRow.split(", ");
            int tbuid = Integer.parseInt(dataArray[0]);
            String t_title = dataArray[1];
            String tfirst = dataArray[2];
            String tlast = dataArray[3];
            String tdescription = dataArray[4];
            String tisbn = dataArray[5];
            String tdpurchase = dataArray[6];
            String tcopyrightyear = dataArray[7];
            double tcost = Double.parseDouble(dataArray[8]);
            dataRow = CSVFile.readLine();
            int crYear = Integer.parseInt(tcopyrightyear);

            Book tempBook = new Book(tbuid, t_title, tfirst, tlast, tdescription, tisbn, tdpurchase, crYear, tcost);
            BookList.add(tempBook);
        }
        CSVFile.close();
    } catch(IOException fnfe) { 
        System.out.println(fnfe.getMessage());
    }
}
public void readForCustomer(){      
    try{ 
        BufferedReader CSVFile = new BufferedReader(new FileReader("customer.csv"));

        String customerRow = CSVFile.readLine();
        customerRow = CSVFile.readLine();
        while (customerRow != null){
            String[] customerCSVArray = customerRow.split(", ");
            int tcuid = Integer.parseInt(customerCSVArray[0]);

            String temp_zip = customerCSVArray[7];
            int tzip = Integer.parseInt(temp_zip);
            String temp_balance = customerCSVArray[8];
            double tbalance = Double.parseDouble(temp_balance);


            Customer tempCustomer = new Customer(tcuid, customerCSVArray[1], customerCSVArray[2], customerCSVArray[3], customerCSVArray[4], customerCSVArray[5], customerCSVArray[6], tzip, tbalance, customerCSVArray[9]);
            CustomerList.add(tempCustomer);
        }
        CSVFile.close();
    } catch(IOException fnfe) { 
        System.out.println(fnfe.getMessage());
    }
} 
Run Code Online (Sandbox Code Playgroud)

这是问题:第一个,readForBook,完美地工作,但是,readForCustomer是具有"线程中的异常"主"java.lang.OutOfMemoryError:Java堆空间"错误的那个:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.nio.HeapByteBuffer.<init>(HeapByteBuffer.java:39)
at java.nio.ByteBuffer.allocate(ByteBuffer.java:312)
at sun.nio.cs.StreamDecoder.<init>(StreamDecoder.java:231)
at sun.nio.cs.StreamDecoder.<init>(StreamDecoder.java:211)
at sun.nio.cs.StreamDecoder.forInputStreamReader(StreamDecoder.java:50)
at java.io.InputStreamReader.<init>(InputStreamReader.java:57)
at java.util.Scanner.<init>(Scanner.java:590)
at Customer.<init>(Customer.java:6)
at Library.readForCustomer(Library.java:219)
at Library.importFromTextFile(Library.java:170)
at Library.mainMenu(Library.java:149)
at Library.<init>(Library.java:17)
at runIt.main(runIt.java:3)
Run Code Online (Sandbox Code Playgroud)

我通过VisualVM运行堆转储,并且它说有大量的字符串实例.

Rad*_*zea 6

在你的while循环中,你必须继续打电话CSV.readLine ().你只在循环外调用一次,这意味着你最终会得到一个无限循环,可能还有一个完整的堆.