java - BufferedReader 字符串内存泄漏?

标签 java string memory bufferedreader

我正在为 AP Computer Science 做一个项目,我们基本上是在做一个图书馆,你可以在里面添加客户和书籍,向客户借书等等,我们必须能够读/写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());
    }
} 

这里是问题所在:第一个 readForBook 工作得很好,但是 readForCustomer 是一个有“Exception in thread “main”java.lang.OutOfMemoryError: Java heap space”的错误:

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)

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

最佳答案

在您的while 循环中,您必须不断调用CSV.readLine ()。您只能在循环之外调用它一次,这意味着您最终会遇到一个无限循环,并且可能是一个完整的堆。

关于java - BufferedReader 字符串内存泄漏?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15950326/

相关文章:

java - 字符串转int的方法

c - 从数组读取性能

java - 如何知道在 Spring 2.0 Controller 方法中当前 servlet 上下文中映射了哪些 URL?

java - 我的变量没有正确初始化

c++ - 如何更改 C++ 中的 LPCTSTR 对象?

c - 在 c 中用另一个字符串替换一部分字符串的最佳方法?

c++ - 未初始化的变量有什么用处吗?

Mysql- "SELECT field "确实将内存填充为 "SELECT * "吗? - 因诺数据库

java - 时区转换和日期显示不正确

java - TCP套接字中的IOException和EOFException有什么区别