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.
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?
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
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.