Unexpected memory using when using Postgres jdbc driver
<p>I am trying to execute a query using postgre sql driver for java jdbc.</p>

<p>I have an issue with memory buildup my statement is in a loop and then sleeps.</p>

<p>The problem is when I look at the job in task manager I can see the memory climbing 00,004K at a time. I have read the documentation I have closed all connections statements resultsets but this still happens.</p>

<p>Please could you tell me what is causing this in my code.</p>

<pre><code>String sSqlString =  new String("SELECT * FROM stk.comms_data_sent_recv " +
                "WHERE msgtype ='RECEIVE' AND msgstat ='UNPRC' " +
                "ORDER BY p6_id,msgoccid " +
                "ASC; ");

        ResultSet rs = null;


        Class.forName("org.postgresql.Driver");

        Connection connection = DriverManager.getConnection(
                "jdbc:postgresql://p6tstc01:5432/DEVC_StockList?autoCloseUnclosedStatements=true&amp;logUnclosedConnections=true&amp;preparedStatementCacheQueries=0&amp;ApplicationName=P6Shunter", "P6dev",
                "admin123");

        //Main Loop 
        while(true)
        {


            try{    

                Statement statement = connection.createStatement();
                statement.executeQuery(sSqlString);
                //rs.close();   
                statement.close();  

                //connection.close();
                rs = null;
                //connection = null;
                statement =null;
            } 
            finally {
            //connection.close();
            }


            try {
                Thread.sleep(loopTime);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }


    }
</code></pre>

<p>Notice the commented out code.. I did close all but that did not seem to make a difference. Whet I did see is that it seems that the statement <code>executeQuery(sSqlString);</code> is causing this the reason I think so is if I remove the statement there is no memory leak.</p>

<p>I could be wrong but please assist me.</p>

<p><strong>UPDATE:</strong></p>

<p>I have changed my code as with your recommendations. Hope its a bit better please let me know if I need to change something.</p>

<p>My main loop :</p>

<pre><code>public static  void main(String[] args) throws Exception {
    // TODO Auto-generated method stub


        //Main Loop 
        while(true)
        {

            getAndProcessAllUnprcMessagesFromStockList();

            try {
                Thread.sleep(loopTime);
            } catch (InterruptedException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }

        }


}
</code></pre>

<p>My Function it will call do fetch data :</p>

<pre><code>public static void getAndProcessAllUnprcMessagesFromStockList() throws Exception
{

    ResultSet rs = null;
    Statement statement = null;
    Connection connection =null;

    String sSqlString =  new String("SELECT * FROM stk.comms_data_sent_recv " +
                        "WHERE msgtype ='RECEIVE' AND msgstat ='UNPRC' " +
                        "ORDER BY p6_id,msgoccid " +
                        "ASC; ");


    try{
        Class.forName("org.postgresql.Driver");

         connection = DriverManager.getConnection(
                "jdbc:postgresql://p6tstc01:5432/DEVC_StockList?autoCloseUnclosedStatements=true&amp;logUnclosedConnections=true&amp;preparedStatementCacheQueries=0&amp;ApplicationName=P6Shunter", "P6dev",
                "admin123");

         PreparedStatement s = connection.prepareStatement(sSqlString,
                    ResultSet.TYPE_SCROLL_INSENSITIVE,
                    ResultSet.CONCUR_READ_ONLY);

         rs = s.executeQuery();


         while (rs.next()) {

                //Process records
                UnprcMsg msg = new UnprcMsg();


                msg.setP6Id(rs.getString(1));
                msg.setMsgOccId(rs.getString(2));
                msg.setWsc(rs.getString(3));
                msg.setMsgId(rs.getString(4));
                msg.setMsgType(rs.getString(5));
                msg.setMsgStatus(rs.getString(6));


                //JOptionPane.showMessageDialog(null,msg.getP6Id(), "InfoBox: " + "StockListShunter", JOptionPane.INFORMATION_MESSAGE);
                //msg2 = null;

            }



         rs.close();

         s.close(); 
    }
    catch(Exception e)
    {
        e.printStackTrace();
    }
    finally
    {
        connection.close();
    }


}
</code></pre>

<p>I have closed my connections statements and results.
I also downloaded eclipse memory analyzer and I ran the jar witch will execute my main loop. Ran it for about an hour and here's some of the data I got from memory analyzer..</p>

<p>Leak suspects :</p>

<p><a href="https://i.stack.imgur.com/Gorxw.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/Gorxw.png" alt="enter image description here"></a></p>

<p><a href="https://i.stack.imgur.com/0yuAl.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/0yuAl.png" alt="enter image description here"></a></p>

<p><a href="https://i.stack.imgur.com/inRZv.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/inRZv.png" alt="enter image description here"></a></p>

<p>Now I know I cant go on the memory usage of task manager but whats the difference? Why does task manager show the following :</p>

<p><a href="https://i.stack.imgur.com/v5YNt.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/v5YNt.png" alt="enter image description here"></a></p>

<p>I was concerned about the memory usage I see in task manager? should I be? </p>