我在基于Swing的应用程序中遇到了一些性能问题我一直负责维护 - 我怀疑是内存泄漏。分析之后,似乎在主应用程序类(即入口点)中花费了非常大量的时间,具体而言,在传递对其自己的应用程序对象的引用的方法中,如下所示:Java/Swing传递应用程序对象
public synchronized static ProblemApplication getApplication() {
if (s_Instance == null) {
initializeInstance();
}
return (ProblemApplication) s_Instance;
}
private synchronized static void initializeInstance() {
s_Instance = Application.getInstance();
}
这就是所谓的很多整个代码 - 一个典型的用法:
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()));
}
}
,并与一些动作侦听器:
// 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();
}
}
});
反正我有很少的摇摆,以及我的Java(尤其是这种东西)有点生疏。
这种使用同步的单例应用程序对象在这种环境中是否合法?
我知道它的特殊用法可能会导致问题,即使方法是正确的,我想我只是想知道这是否是我的问题的可能候选人,并且我应该进一步调查。这种用法对我来说是错误的 - 但那可能就是我!
感谢您的帮助。
可能重复的[如何找到Java内存泄漏](http://stackoverflow.com/questions/40119/how-to-find-a-java-memory-leak) – DavidPostill
感谢您的链接。类似的问题(我会按照建议的流程),但IMO不完全是重复的。 我想这个问题的一个更好的标题就像'正在访问一个同步的Swing应用程序实例最佳实践? 但是看到链接过程产生什么会很有趣。 – Caligari