Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7586827
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T19:31:20+00:00 2026-05-30T19:31:20+00:00

Last summer, I made a Java application that would parse some PDF files and

  • 0

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.

Everything was fine and I kept adding new files to the database every week or so without any problems.

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: OutOfMemoryError: Java Heap Space. 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.

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.

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.

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?

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.

Edit
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:

public static void visitAllDirsAndFiles(File dir){
    if (dir.isDirectory()){
        String[] children = dir.list();

        for (int i = 0; i < 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);
        }
    }
}

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 BowlingFilesReader.readFile(dir, playersDatabase);

The memory should be released when the method is done I think?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T19:31:22+00:00Added an answer on May 30, 2026 at 7:31 pm

    Your first instinct on open resultsets and connections was good, though maybe not entirely the cause. Let’s start with your database connection first.

    Database

    Try using a database connection pooling library, such as the Apache Commons DBCP (BasicDataSource is a good place to start): http://commons.apache.org/dbcp/
    You will still need to close your database objects, but this will keep things running smoothly on the database front.


    JVM Memory

    Increase the size of the memory you give to the JVM. You may do so by adding -Xmx and a memory amount after, such as:

    • -Xmx64m <- this would give the JVM 64 megs of memory to play with
    • -Xmx512m <- 512 megs

    Be careful with your numbers, though, throwing more memory at the JVM will not fix memory leaks. You may use something like JConsole or JVisualVM (included in your JDK’s bin/ folder) to observe how much memory you are using.

    Threading

    You may increase the speed of your operations by threading them out, assuming the operation you are performing to parse these records is threadable. But more information might be necessary to answer that question.

    Hope this helps.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After doing some work with Ruby, Rails, and RSpec last summer and I learned
Ever since British Summer Time ended in the UK last week my application has
Last day I faced an interview and they asked me java questions among which
Last year, Scott Guthrie stated You can actually override the raw SQL that LINQ
Last time I asked about the reverse process , and got some very efficient
I've spent the last year learning and using Java in university but we didn't
Is it possible to make a PHP application think that the server datetime is
I am practicing over the summer to try and get better and I am
Last week, we created a program that manages sets of strings, using classes and
Last night I wrote up my first IHttpModule to do some request processing. I'm

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.