Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BufferedReader string memory leak?

I'm working on a project for AP Computer Science where we are essentially making a library, where you can add customers and books, check out books to customers, etc, and we have to be able to read/write to a CSV. I'm having some trouble with this code (this is only part of it, and I am pretty sure the rest of it works, the whole thing is approaching 600 lines, total):

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());
    }
} 

Here's the issue: the first one, readForBook, works perfectly, however, readForCustomer is the one that has the "Exception in thread "main" java.lang.OutOfMemoryError: Java heap space" error:

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)

I ran a heap dump through VisualVM, and it's saying that there are a large amount of string instances.

like image 928
Jack Sampson Avatar asked Feb 16 '23 20:02

Jack Sampson


1 Answers

In your while loop, you have to keep calling CSV.readLine (). You call it only once outside of the loop, which means you'll end up with an infinite loop and possibly a full heap.

like image 163
Radu Murzea Avatar answered Feb 19 '23 10:02

Radu Murzea