I think I have a memory leak, although I'm not sure if I'm interpreting any of this correctly. I am running an application that queries an SQL database for information that I subsequently put into two separate JTables.
This works fine.
I created a custom class for searching the tables that extends JTextField, based on a neat method I found on the interweb.
This is where I have problems.
I have found that if I create the JSearchBar object and DO NOT add it to the frame, I don't run into any issues. However, if I create the JSearchBar and add it to the JFrame, once displayed the memory usage of my java application climbs and climbs and climbs. With the JSearchBar displayed, CPU usage will bounce from 0 to 1. Without it on the frame, it stays at 0.
[edit] I have also noticed that if I my JFrame does not have focus, or if it has focus but the search bar is not selected, memory usage still increases, but only by about 10K at a time, as opposed to hundreds of K of a time. [/edit]
Here is the code for my custom class. At one point, I added some System.out.println(); to each listener method, and nothing was happening, so I'm not sure why the memory is climbing.
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.table.*;
public class JSearchBar extends JTextField
{
JTable[] t;
public JSearchBar()
{
super("No Associated Tables", 11);
}
public JSearchBar(JTable... t)
{
super(10);
this.getDocument().addDocumentListener(new searchHandler());
this.t = t;
}
private class searchHandler implements DocumentListener
{
public void changedUpdate(DocumentEvent e)
{
System.out.println("changedUpdate");
}
public void insertUpdate(DocumentEvent e)
{
try
{
int L = e.getDocument().getLength();
String text = e.getDocument().getText(0,L);
newFilter(text);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
public void removeUpdate(DocumentEvent e)
{
try
{
int L = e.getDocument().getLength();
String text = e.getDocument().getText(0,L);
newFilter(text);
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
private void newFilter(String filterText)
{
RowFilter<MyTableModel, Object> rf = null;
//If current expression doesn't parse, don't update.
try {
rf = RowFilter.regexFilter("(?iu)" + filterText);
} catch (java.util.regex.PatternSyntaxException e) {
return;
}
for(int i = 0; i < t.length; i++)
{
TableRowSorter<MyTableModel> trs = (TableRowSorter<MyTableModel>)t[i].getRowSorter();
trs.setRowFilter(rf);
}
}
}
}
Here is a couple graphs from my VisualVM
Thanks for any input!!
[EDIT2}
I replaced the JSearchBar with a JTextField, and it has the same behavior. The good news is that I didn't cause the memory leak. But, I'd still like to know why the JTextField is consuming memory like this.
[UPDATE]
Here is the updated graphic of my heap. Not sure what's going on here.