Note that there are some explanatory texts on larger screens.

plurals
  1. POReading in large text files, problems with the garbage collector and Scanner object
    primarykey
    data
    text
    <p>I am writing a program that needs to read in very large files (about 150Mb of text). I am running into an out of memory error when I try to read in files that are larger than 50Mb. Here is an excerpt from my code.</p> <pre><code>if (returnVal == JFileChooser.APPROVE_OPTION) { file = fc.getSelectedFile(); gui.setTitle("Fluent Helper - " + file.toString()); try{ scanner = new Scanner(new FileInputStream(file)); gui.getStatusLabel().setText("Reading Faces..."); while(scanner.hasNext()){ count++; if(count&lt;1000000){ System.gc(); count = 0; } readStr = scanner.nextLine()+ "\n"; if(readStr.equals("#\n")){ isFaces = false; gui.getStatusLabel().setText("Reading Cells..."); }else if(isFaces){ faces.add(new Face(readStr)); }else{ cells.add(new Cell(readStr)); } } }catch (Exception e){ e.printStackTrace(); }finally{ try{ scanner.close(); }catch(Exception e){ e.printStackTrace(); } } System.out.println("flie selected"); } else { System.out.println("file not selected"); } </code></pre> <p>the small block that calls the garbage collector every arbitrary number of reads is something I added to solve the memory problem, but it doesn't work. Instead the program hangs and never gets to the cells portion of the file (which should happen in less than a second). Here is the block.</p> <pre><code> if(count&lt;1000000){ System.gc(); count = 0; } </code></pre> <p>My guess is that maybe the Scanner's pointer is getting garbage collected or something. I really don't have any clue. Launching the program with a larger heap is not really an option for me. The program should be usable by people with out very much computer knowledge.</p> <p>I would like a solution to get the file in with out a problem, be it a memory management one or fixing the scanner or a more efficient means of reading the file. Thanks everyone.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload