Memory leak with Inner Classes like ClickListener

While researching why I had such a bad memory leak,
I found out that Inner classes can cause them
, so I was trying to convert my ClickListener subclass to a class file of its own, but I’m having a bit of trouble (?)

I have a button on my app that pulls data from a database and fills in a grid. I think this is called an inner class.

[code]
BeanItemContainer<String,bean> beanItemContainer =
new BeanItemContainer<String, bean>(bean.class);
beanitemContainer.setBeanIdProperty(“TID”);

dateButton.addClickListener(new ClickListener() {
private static final long serialVersionUID = 1L;

        @Override
        public void buttonClick(ClickEvent event){
            dateButton.setEnabled(false);
            beanItemContainer.removeAllItems();
            grid.setSelectionMode(SelectionMode.NONE);
            grid.setSelectionMode(SelectionMode.MULTI);

            try {
                //Connect to database, pull all data, and add it to beanItemContainer
               
            } catch (ClassNotFoundException | SQLException e) {
                e.printStackTrace();
            }
            //Enables the button after process is done
            dateButton.setEnabled(true);
        } //End Click Listener
    }); //End Click Event

[/code]This is a really stupid question, but how do I pass the beanItemContainer to the clicklistener to modify, and then bring it back? Is it like this?:

[code]
public class DateButtonClickListener implements Button.ClickListener {
private BeanItemContainer<String,bean> beanItemContainer;

public DateButtonClickListener(BeanItemContainer<String,bean> beanItemContainer){
    
}

@Override
public BeanItemContainer<String,bean> buttonClick(ClickEvent event) {
    //Get data from database, fill in beanItemContainer
     return beanItemContainer;
    
}

}
[/code]Sorry I’m having trouble with a simple topic.

Inner class
https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
are different from anonymous classes
https://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
which you are passing there to addClickListener method.

Regarding your DateButtonClickListener, you are already modifying the BeanItemContainer instance by a reference, no need to return it.

So instead of making it an anonymous class, do you think just keeping it a static inner class, it’ll help with the memory leak? The leak is actually pretty big in my opinion, It goes up by 150-200mb every call, and Garbage collector doesn’t seem to work on it.

Are you sure that the memory leak is not in the way you handle database connection and data after that. Can you post the code you are executing in the click listener?

dateButton.addClickListener(new ClickListener() {
            private static final long serialVersionUID = 1L;
            @Override
            public void buttonClick(ClickEvent event){
                beanItemContainer.removeAllItems();
                try {
                    
                    Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
                    Connection conn=DriverManager.getConnection("jdbc:ucanaccess://\\\\server\\folder\\AccessDatabase.mdb");
                     Statement s = conn.createStatement();
                     ResultSet rs = s.executeQuery("SELECT [PLANT]
, [DEPARTMENT]
, [Entry-dt]
, [TID]
 FROM [Query1]
 where [RESOURCE]
 NOT LIKE 'Example*' AND [Entry-dt]
 = #"+new SimpleDateFormat("MM/dd/yyyy").format(df.getValue())+"#");
                 
                     while (rs.next()) {
                         beanItemContainer.add(new Bean(rs.getString(1),rs.getString(2),rs.getString(3));
                     }
                     rs.close();
                     s.close();
                     conn.close();
                } catch (ClassNotFoundException | SQLException e) {
                    e.printStackTrace();
                }
                
            }
        });

Do you get any exceptions when that code is run? If anything throws an exception, then the last rows which calls close will not be run - it exits the try-catch. You should have a finally-block after catch where you do the clean-up

try {
  ...
} catch (ClassNotFoundException | SQLException e) {
  ...
} finally {
  rs.close();
  s.close();
  conn.close();
}

Not sure if all the close methods are needed or if one is enough

I only get an out of memory exception when if I use it a certain amount of times, (usually about 5-6 times) then I have to restart the run. I’ll give this a try.