Java JFrame内存泄漏?
所以今天我打开任务管理器,发现我的应用程序每秒泄漏200kbs内存。我查看了我的主循环:Java JFrame内存泄漏?,java,swing,memory-leaks,thread-sleep,invokelater,Java,Swing,Memory Leaks,Thread Sleep,Invokelater,所以今天我打开任务管理器,发现我的应用程序每秒泄漏200kbs内存。我查看了我的主循环: public final void run() { try { Thread.sleep(27); } catch (InterruptedException e1) { e1.printStackTrace(); } Thread curThread = Thread.currentThread(); long lastLoopTi
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);
}
我似乎不明白为什么它一直在泄漏内存?
对此内存泄漏的任何提示或解决方案都会很有帮助
问候,,
tambre看看这部分节目:
while (thread == curThread) {
if (shouldClose)
{
running = false;
frame.dispose();
thread = null;
curThread.interrupt();
curThread = null;
// HERE
}
// ...
}
问题:在我在这里标记的点上,thread==curThread将是什么
答:对
问题:循环是否会终止
回答:不
老实说,这部分代码是一团糟。你似乎在使用两到三种不同的机制试图杀死。。。某物最后,调用SwingUtilities.invokelater,我认为这将启动另一个线程来再次运行Runnable。只是。。。不可理解。看看程序的这一部分:
while (thread == curThread) {
if (shouldClose)
{
running = false;
frame.dispose();
thread = null;
curThread.interrupt();
curThread = null;
// HERE
}
// ...
}
问题:在我在这里标记的点上,thread==curThread将是什么
答:对
问题:循环是否会终止
回答:不
老实说,这部分代码是一团糟。你似乎在使用两到三种不同的机制试图杀死。。。某物最后,调用SwingUtilities.invokelater,我认为这将启动另一个线程来再次运行Runnable。只是。。。无法理解。在划掉这些行之后:
fpsLabel.setText("FPS: " + fps);
fpsDone.setText("FPS: " + fps);
内存泄漏似乎被堵塞了。
为什么setText正在泄漏内存?
这个问题有点被回答了,但仍然是为什么?在划掉以下几行之后:
fpsLabel.setText("FPS: " + fps);
fpsDone.setText("FPS: " + fps);
内存泄漏似乎被堵塞了。
为什么setText正在泄漏内存?
这个问题已经回答了,但仍然是为什么?SwingUtilities.invokeLater在EDT上运行Runnable。如果runnable从未返回,如您在此处所述,则无法处理进一步的事件请创建一个演示问题并更新您的问题。从此处发布的代码中不可能,为了获得更好的帮助,请尽快发布SSCCE,简称runnable,CompileableFyi无论从invokeLater调用什么,被Thread.sleepint终止,都将保留在JVM内存中,直到Thread.sleepint结束。请创建一个演示问题并更新您的问题。从此处发布的代码来看,这是不可能的,为了获得更好的帮助,请尽快发布SSCE,简短,可运行,CompilebleFYI无论从invokeLater调用什么,都会被Thread.sleepint终止,直到Thread.sleepint终止,它都会保留在JVM内存中ended@tambre-我想你应该扔掉整个方法,重新开始!循环将终止,因为稍后它将返回ifshouldClose@RussellZahniser-好吧,但还是一团糟。@tambre-我想你应该扔掉整个方法,重新开始!循环将终止,因为稍后它将返回ifshouldClose@RussellZahniser——好吧,但还是一团糟。