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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:17:12+00:00 2026-05-17T15:17:12+00:00

I am experiencing a memory leak with a code similar to the one below

  • 0

I am experiencing a memory leak with a code similar to the one below (it’s a simulation with different inputs at every loop).

The problem

The object Object_XXX is quite complex, with connections to databases and other objects populated with data from databases aswell.

    for(int i=0; i<MAX; i=i+1){
        Class_XXX Object_XXX = new Class_XXX(Arg_1, Arg_2);

        // action with Object_XXX
    }

Now, after calling a couple of methods, Object_XXX can also be discarded since the next loop will requires an object with different characteristics (different arrays, size of the arrays, nested objects…).

The constructor is similar to the one below, and the other classes have a similar constructor.

public Class_XXX(Arg_1, Arg_2, DB_Connection){

    try {
        Statement Query_Statement = null;
        ResultSet Query_ResultSet = null;
        String Query_String = null;

        Query_String = "...";
        Query_Statement = DB_Connection.createStatement();
        Query_ResultSet = Query_Statement.executeQuery(SQL);

        while (Query_ResultSet .next()) {
            this.Param_1 = Query_ResultSet .getString("param_1");
    this.Param_2 = Query_ResultSet .getString("param_2");
            ...
    this.Param_n = Query_ResultSet .getString("param_n");
        }
    } catch (SQLException e) {
        System.out.println("SQL Exception: "+ e.toString());
    }
}

Questions

What would be the most correct approach in this case?
a) to finalize Object_XXX at the end of the loop
b) to finalize every single object that compose Object_XXX when they are not used inside the code?
Personally I would prefer a) since I think this would leave the garbage collector to work without messing too much with it

Could you also provide a code example or a reference?

Thanks!


Second round:

After the answers found below and a look at this other page (http://accu.org/index.php/journals/236), this is the template that i’m using now for the constructors. Too early to see if it works. There is still the “exception.toString” but the real code gives to the variables standard values in the case of an exception and reports the action in a log.

public Class_XXX(String Object_Name, java.sql.Connection Query_Connection){

    try{ // begin try-catch for Query_Connection
        Statement Query_Statement = Query_Connection.createStatement();
        try { // begin try-finally for Query_Statement
            String Query_String = "SELECT param_1, param_2, ... param_3 FROM table_name WHERE object_name = '" + Object_Name + "'";
            ResultSet Query_ResultSet = Query_Statement.executeQuery(Query_String);
            try { // begin try-finally for Query_ResultSet

                while (Query_ResultSet.next()) {
                    this.Param_1 = Query_ResultSet.getString("param_1");
                    this.Param_2 = Query_ResultSet.getString("param_2");
                    // ...
                    this.Param_n = Query_ResultSet.getString("param_n");
                }

            } finally {
                try { Query_ResultSet.close(); }
                catch (SQLException ex) { System.out.println("Error in Class_XXX constructor - " + ex.toString()); }
            } // end try-finally for Query_ResultSet

        } finally {
            try { Query_Statement.close(); }
            catch (SQLException ex) { System.out.println("Error in Class_XXX constructor - " + ex.toString()); }
        } // end try finally for Query_Statement

    } catch(SQLException ex) {
            System.out.println("Error in Class_XXX constructor - " + ex.toString());
    } // end try-catch for Query_Connection

}
  • 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-17T15:17:12+00:00Added an answer on May 17, 2026 at 3:17 pm

    Looks like you are a C++ programmer used to doing RAII.

    Java does not support RAII with equivalent semantics as C++, as object destruction is done by the garbage collector, sometime after the object becomes unreachable, in a seperate background thread. Because of this, hardly anyone uses the finalize method (which would otherwise be the equivalant of a C++ destructor) to release resources.

    For objects that only occupy memory, Java does not need RAII, as their memory will automatically be reclaimed by the garbage collector sometime after they become unreachable. In particular, explicitly freeing members is not necessary.

    Objects managing resources other than memory (such as file descriptors), or wishing to perform immediate cleanup work usually offer a cleanup-method, which must be invoked explicitly. As you can see from their javadoc, instances of Statement and ResultSet are such objects (they refer to resources outside the vm, which one wants to release in a timely fashion). The typical pattern to invoke the cleanup method in an exception-safe manner is:

    Statement statement = connection.createStatement();
    try {
        ResultSet resultset = statement.executeQuery(sql);
        try {
            // read the resultset
        } finally {
            resultset.close();
        }
    } finally {
        statement.close();
    }
    

    And a few matters of style:

    • exception.toString() merely contains the exception message. exception.printStackTrace() additionally prints the entire stacktrace.
    • nearly every java programmer follows the convention that package names begin with a lowercase letter, class names with an upper case letter, and fields/variables with lowercase letters. Also, words are usually separated using camel-case rather than _.
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Our team has been experiencing a recurring problem with velocity templates. Upon rendering, some
I've just started experimenting with SDL in C++, and I thought checking for memory
After experiencing numerous problems with a certain media player, it got me thinking is
I'm experiencing an issue on a test machine running Red Hat Linux (kernel version
I'm experiencing what I believe is a circular dependency issue with my PHP application.
I'm experiencing an issue with ActiveMQ and would like to trace/view all ActiveMQ activity.
I'm experiencing a strange issue where my Visual Studio 2005 C++ program crashes the
I've been experiencing the good and the bad sides of messaging systems in real
I am experiencing a crash while using the Boost.Spirit and Boost.Thread libraries in my
We are experiencing some slowdowns on our web-app deployed on a Tomcat 5.5.17 running

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.