简体   繁体   English

JFrame内存泄漏?

[英]JFrame leaking memory?

So today I opened Task Manager and saw that my application leaks 200kbs of memory every second. 因此,今天我打开了任务管理器,发现我的应用程序每秒泄漏200kbs的内存。 I looked at my main loop: 我看了看我的主循环:

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);
}

And I can't seem to figure out why it keeps leaking memory? 而且我似乎无法弄清楚为什么它会不断泄漏内存? Any hints or solution to this memory leak would be helpful! 任何有关此内存泄漏的提示或解决方案都将有所帮助!

Regards, tambre 问候,Tambre

Look at this part of the program: 看一下程序的这一部分:

 while (thread == curThread) {
    if (shouldClose)
    {
        running = false;
        frame.dispose();
        thread = null;
        curThread.interrupt();
        curThread = null;
        // HERE
    }
    // ...
 }

Question: What will thread == curThread be at the point I labeled "HERE"? 问题:在我标记为“ HERE”时, thread == curThread将是什么?

Answer: True. 答:是的。

Question: And will the loop terminate? 问题:循环会终止吗?

Answer: No! 答:不!


To be brutally honest, this part of your code is a mess. 老实说,代码的这一部分是一团糟。 You seem to be using two or three different mechanisms to attempt to kill ... something. 您似乎正在使用两种或三种不同的机制来尝试杀死某物。 Finally, you call SwingUtilities.invokeLater(this) which will (I think) just launch another thread to run the Runnable again. 最后,您调用SwingUtilities.invokeLater(this) ,它将(我认为)只是启动另一个线程来再次运行Runnable。 It's just ... incomprehensible. 只是...难以理解。

JLabel.setText() calls repaint, which pushes a PaintEvent onto the event queue. JLabel.setText()调用repaint,这会将PaintEvent推送到事件队列中。 Because your loop is stalling the event queue, it grows infinitely. 因为您的循环使事件队列停滞,所以它无限增长。 Hence the memory leak. 因此内存泄漏。

( SwingUtilities.invokeLater() runs a Runnable on the EDT. If that runnable never returns, like yours here, then no further events can be processed) SwingUtilities.invokeLater()在EDT上运行一个Runnable。如果该Runnable从不返回,就像您在此处那样,则无法处理其他事件)

After commething out these lines: 整理完以下几行之后:

fpsLabel.setText("FPS: " + fps);
fpsDone.setText("FPS: " + fps);

The memory leaks seems like plugged. 内存泄漏似乎已堵塞。 Why is setText() leaking memory? 为什么setText()泄漏内存? Question is kinda answered, but still why? 问题已经回答了,但是为什么呢?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM