The*_*dex 2 java memory garbage-collection memory-leaks
我的java游戏应用程序中存在内存泄漏,我有点期待.泄漏来自在此按钮动作侦听器上多次创建的新实例,因为每次按下按钮都会创建一个新实例RegularMode:
btnRegular.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
frame.remove(pane);
gm = Gamemode.REGULAR;
mode = new RegularMode(frame, WIDTH, HEIGHT);
}
});
Run Code Online (Sandbox Code Playgroud)
有趣的是,我一直试图修复内存泄漏,使用此代码:
public static void initDisplay() {
gm = Gamemode.NONE;
mode.setRunning(false);
frame.remove(mode.getPane());
frame.add(pane);
frame.validate();
frame.repaint();
mode = null; // THIS LINE
frame.pack();
}
Run Code Online (Sandbox Code Playgroud)
- 但它不起作用.有没有其他方法可以解决这种类型的内存泄漏?
I am not sure how you came to concluding that the code you provided is causing the memory leak. Use some profiler to see what objects are currently in heap and accumulating. You can search for profilers or check this: http://jyops.blogspot.se/2012/09/java-memory-model-simplified.html