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 6860419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:24:24+00:00 2026-05-27T02:24:24+00:00

My application has a memory leak resulting from my usage of JDBC. I have

  • 0

My application has a memory leak resulting from my usage of JDBC. I have verified this by looking at a visual dump of the heap and seeing thousands of instances of ResultSet and associated objects. My question, then, is how do I appropriately manage resources used by JDBC so they can be garbage collected? Do I need to call “.close()” for every statement that is used? Do I need to call “.close()” on the ResultSets themselves?

How would you free the memory used by the call:

ResultSet rs = connection.createStatement().executeQuery("some sql query");

??

I see that there are other, very similar, questions. Apologies if this is redundant, but either I don’t quite follow the answers or they don’t seem to apply universally. I am trying to achieve an authoritative answer on how to manage memory when using JDBC.

::EDIT:: Adding some code samples

I have a class that is basically a JDBC helper that I use to simplify database interactions, the main two methods are for executing an insert or update, and for executing select statements.

This one for executing insert or update statements:

public int executeCommand(String sqlCommand) throws SQLException {
    if (connection == null || connection.isClosed()) {
        sqlConnect();
    }
    Statement st = connection.createStatement();
    int ret = st.executeUpdate(sqlCommand);
    st.close();
    return ret;
}

And this one for returning ResultSets from a select:

public ResultSet executeSelect(String select) throws SQLException {
    if (connection == null || connection.isClosed()) {
        sqlConnect();
    }
    ResultSet rs = connection.createStatement().executeQuery(select);
    return rs;
}

After using the executeSelect() method, I always call resultset.getStatement().close()

Examining a heap dump with object allocation tracing on shows statements still being held onto from both of those methods…

  • 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-27T02:24:24+00:00Added an answer on May 27, 2026 at 2:24 am

    You should close the Statement if you are not going to reuse it. It is usually good form to first close the ResultSet as some implementations did not close the ResultSet automatically (even if they should).

    If you are repeating the same queries you should probably use a PreparedStatement to reduce parsing overhead. And if you add parameters to your query you really should use PreparedStatement to avoid risk of sql injection.

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

Sidebar

Related Questions

My application has multiple SQLite tables(around 20 with different fields). This I have created
My Application has 3 activities (MainActivity,SampleActivity,TempActivity) and Application have to start from MainActivity because
Consider an ASP.NET application with has a connection pool memory leak problem (where connections
I'm getting some unusual memory leak in a native c++ win32 application. It has
I have been looking into a memory leak issue for a while. I found
Is there a good application (that has some kind of gui) for testing memory
The application has been put in iCloud since the beginning, so I have the
I have a strange (to me) memory leak when accessing an entity in a
How do I compare memory heap dumps in Netbeans? What I have done is
I have been getting a weird memory leak and i just pinpointed what is

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.