Note that there are some explanatory texts on larger screens.

plurals
  1. PODatabase connections and OutOfMemoryError: Java Heap Space
    primarykey
    data
    text
    <p>Last summer, I made a Java application that would parse some PDF files and get the information they contain to store them in a SQLite database.</p> <p>Everything was fine and I kept adding new files to the database every week or so without any problems.</p> <p>Now, I'm trying to improve my application's speed and I wanted to see how it would fare if I parsed all the files I have from the last two years in a new database. That's when I started getting this error: <em>OutOfMemoryError: Java Heap Space</em>. I didn't get it before because I was only parsing about 25 new files per week, but it seems like parsing 1000+ files one after the other is a lot more demanding.</p> <p>I partially solved the problem: I made sure to close my connection after every call to the database and the error went away, but at a huge cost. Parsing the files is now unbearably slow. As for my ResultSets and Statements / PreparedStatements, I'm already closing them after every call.</p> <p>I guess there's something I don't understand about when I should close my connection and when I should keep re-using the same one. I thought that since auto-commit is on, it commits after every transaction (select, update, insert, etc.) and the connection releases the extra memory it was using. I'm probably wrong since when I parse too many files, I end up getting the error I'm mentioning.</p> <p>An easy solution would be to close it after every x calls, but then again I won't understand why and I'm probably going to get the same error later on. Can anyone explain when I should be closing my connections (if at all except when I'm done)? If I'm only supposed to do it when I'm done, then can someone explain how I'm supposed to avoid this error?</p> <p>By the way, I didn't tag this as SQLite because I got the same error when I tried running my program on my online MySQL database.</p> <p><strong>Edit</strong> As it has been pointed out by Deco and Mavrav, maybe the problem isn't my Connection. Maybe it's the files, so I'm going to post the code I use to call the function to parse the files one by one:</p> <pre><code>public static void visitAllDirsAndFiles(File dir){ if (dir.isDirectory()){ String[] children = dir.list(); for (int i = 0; i &lt; children.length; i++){ visitAllDirsAndFiles(new File(dir, children[i])); } } else{ try{ // System.out.println("File: " + dir); BowlingFilesReader.readFile(dir, playersDatabase); } catch (Exception exc){ System.out.println("Other exception in file: " + dir); } } } </code></pre> <p>So if I call the method using a directory, it recursively calls the function again using the File object I just created. My method then detects that it's a file and calls <em>BowlingFilesReader.readFile(dir, playersDatabase);</em></p> <p>The memory should be released when the method is done I think?</p>
    singulars
    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.
 

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