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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T20:59:47+00:00 2026-05-26T20:59:47+00:00

The following code sample is inside a for loop that runs about 2 million

  • 0

The following code sample is inside a for loop that runs about 2 million times.

List<String> parameters = new LinkedList<String>();
stmt2 = null;
rs2= null;

//This is line 472
stmt2 = con.prepareStatement("select NAME from TABLE_NAME where FIELD="+ strId);
rs2 = stmt2.executeQuery();

while (rs2.next()) {
    parameters.add(rs2.getString("NAME"));
}

stack trace:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
    at com.mysql.jdbc.PreparedStatement.<init>(PreparedStatement.java:437)
    at com.mysql.jdbc.Connection.clientPrepareStatement(Connection.java:2185)
    at com.mysql.jdbc.Connection.prepareStatement(Connection.java:4782)
    at com.mysql.jdbc.Connection.prepareStatement(Connection.java:4687)
    at consistencyCheck.ConsistencyCheck.parameterCheck(ConsistencyCheck.java:472)
    at consistencyCheck.ConsistencyCheck.performConsistencyCheck(ConsistencyCheck.java:316)
    at consistencyCheck.ConsistencyCheck.main(ConsistencyCheck.java:198)

Please let me know if more information is required.

Thank you.

Thanks everyone for the answers. I will accept BalusC’s answer since he answered first. Unfortunately I cant upvote any other answers due to not enough reputation 🙁

Just a side note to all who suggested to increase memory heap. Increasing the memory heap is something you should never do unless you are 100 % sure that is the only solution to your problem. For example in my problem increasing the heap might ‘solve’ the problem, but the underlying blunder still remains.

  • 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-26T20:59:48+00:00Added an answer on May 26, 2026 at 8:59 pm

    Based on the comments, you seem to be creating the Statement and ResultSet inside the loop but never closing them. You need to close them in the loop as well. This will free up internal resources.

    Also, you are not really taking benefit of the DB cache of the prepared statement. Right now you are string-concatenating the parameter in the SQL string which causes 2M String objects being created instead of 1 String object. Better prepare the statement before the loop.

    try {
        // ...
        statement = connection.prepareStatement("select NAME from TABLE_NAME where FIELD=?");
    
        for ( /* 2M times? */ ) {
            statement.setInt(1, id);
    
            try {
                resultSet = statement.executeQuery();
                // ...
            } finally {
                if (resultSet != null) try { resultSet.close(); } catch (SQLException ignore) {}
            }
        }
    } finally {
        if (statement != null) try { statement.close(); } catch (SQLException ignore) {}
    }
    

    Alternatively, you can also consider to use an IN clause instead. E.g.

    WHERE field IN (1,2,3,4,5);
    

    This is however trickier with placeholders. See also: What is the best approach using JDBC for parameterizing an IN clause?

    Or as a completely different alternative, if necessary with help of a more experienced DB admin / SQL ninja, rewrite the entire thing so that you get exactly the results you need with only one SQL query. Ask if necessary a separate question about that on sql here on SO.

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

Sidebar

Related Questions

I have the following code sample that im trying to wrap my head around
I am new to Emacs, and I have the following code as a sample.
I am new to C++. While trying sample polymorphism code, I found that base
The goal of the following code sample is to read the contents of $target
Given the following code sample: uint8_t i, in, ni; i = in = 2;
I have the following sample code in a VB.NET console application. It compiles and
I have the following sample code which doesn't seem to want to run. import
I have a Dictionary bound to DataGridView by using following sample code. DataGridView bound
I'm following the sample code in CFNetwork Programming Guide , specifically the section on
In the sample code, the line with the 'error comment' gives the following the

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.