I'm having some performance problems on a Swing based application I've been tasked with maintaining - I suspect a memory leak. After profiling, it appears that there is a very large amount of time being spent in the main application class (ie the entry point), specifically in a method that passes a reference to it's own Application object, like this:
public synchronized static ProblemApplication getApplication() {
if (s_Instance == null) {
initializeInstance();
}
return (ProblemApplication ) s_Instance;
}
private synchronized static void initializeInstance() {
s_Instance = Application.getInstance();
}
This is called a lot throughout the code - a typical usage:
private void updateSensorsModel() {
ProblemApplication application = ProblemApplication .getApplication();
int sensorIndex = 0;
m_SensorModels.clear();
// add sensors information
for (SensorConfiguration s : application.getSensorsConfiguration().getSensors()) {
m_SensorModels.add(new SensorModel(sensorIndex, application));
sensorIndex++;
}
// add extra session information
for (ExtraSession es : application.getSession().getExtraSessions()) {
m_SensorModels.add(new SensorModel(-1, application, es.getDeviceID()));
}
}
and with some action listeners:
// listeners
final TechsasSession session = TechsasApplication.getApplication().getSession();
session.addPropertyChangeListener(new PropertyChangeListener() {
@Override
public void propertyChange(PropertyChangeEvent evt) {
if (evt.getPropertyName().equals("sensorsConfiguration")) {
SensorTableModel model = sensorTable.getModel();
model.updateModel();
repaint();
}
}
});
Anyway I've got very little Swing, and my Java (especially this kind of stuff) is a bit rusty.
Is this use of a synchronised singleton application object legitimate in this kind of environment?
I know that particular usages of it could be causing issues even if the approach is sound, I guess I just want to know if this is a likely candidate for my problems and something I should investigate further. The usage feels wrong to me - but that could just be me!
Thanks for your help.