所以今天我打开任务管理器,看到我的应用程序每秒泄漏 200kbs 的内存。我查看了我的主循环:
public final void run() {
try {
Thread.sleep(27);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
Thread curThread = Thread.currentThread();
long lastLoopTime = System.nanoTime();
long OPTIMAL_TIME = 1000000000 / FPS;
int fps = 0;
long lastFpsTime = 0;
while (thread == curThread) {
if (shouldClose)
{
running = false;
frame.dispose();
thread = null;
curThread.interrupt();
curThread = null;
}
long now = System.nanoTime();
long updateLength = now - lastLoopTime;
lastLoopTime = now;
//double delta = updateLength / ((double)OPTIMAL_TIME);
lastFpsTime += updateLength;
fps++;
if (lastFpsTime >= 1000000000) {
System.out.println("FPS: " + fps + "");
fpsLabel.setText("FPS: " + fps);
lastFpsTime = 0;
fps = 0;
}
if (GuiNewProject.createButton.isEnabled() && createProjectDialogIsOpen)
if (GuiNewProject.folder.getText().length() == 0 || GuiNewProject.projectName.getText().length() == 0)
GuiNewProject.createButton.setEnabled(false);
if (!(GuiNewProject.createButton.isEnabled()) && createProjectDialogIsOpen)
if (GuiNewProject.folder.getText().length() > 0 && GuiNewProject.projectName.getText().length() > 0)
GuiNewProject.createButton.setEnabled(true);
//render();
fpsDone.setText("FPS: " + fps);
try {
if (shouldClose) {
return;
}
else
{
Thread.sleep((lastLoopTime - System.nanoTime() + OPTIMAL_TIME) / 1000000);
}
} catch (Exception e) {
e.printStackTrace();
}
}
SwingUtilities.invokeLater(this);
}
而且我似乎无法弄清楚为什么它不断泄漏内存?对此内存泄漏的任何提示或解决方案都会有所帮助!
问候,坦布尔