本文介绍了越来越消耗内存的简单Java程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个简单的 Java 代码,它创建一个 JFrame 实例并显示它.此链接包含 jconsole 截取的内存消耗图的截图

I have this simple Java Code which creates a single JFrame instance and displays it. This link contains the screenshot of memory consumption graph taken by jconsole

让我担心的是,任务管理器中的 java.exe 显示内存使用量以每 8-9 秒 4-5 kbs 的速度持续增加.需要帮助

What worries me is that java.exe in task manager shows memory usage continuously increasing at the rate of 4-5 kbs every 8-9 seconds. Need help

import javax.swing.*;

class MyGUI extends JFrame
{
    public void makeGUI()
    {
        setLayout(null);
        setSize(500, 200);
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setVisible(true);
    }
}

public class Launcher
{
    public static void main(String []args)
    {
        SwingUtilities.invokeLater(new Runnable()
                    {
                       public void run()
                       {
                        new MyGUI().makeGUI();
                       }
                    });
    }
}

推荐答案

配置文件看起来完全正常 - 程序创建对象,并且垃圾收集器不时通过清除不再可达的对象来释放内存.

The profile looks perfectly normal - the program creates objects and from time to time, the garbage collector frees memory by getting rid of the objects that are not reachable any longer.

重要的观察是低谷点或多或少都处于同一级别,因此看起来您的代码没有内存管理问题.

The important observation is that the trough points are all more or less at the same level so it does not look like your code has a memory management issue.

您可以通过将最大堆空间量设置为较低的级别来降低峰值的高度,但 5 MB 无论如何都不算​​多...

You could reduce the height of the peaks by setting the maximum amount of heap space to a lower level but 5 MB is not much anyway...

这篇关于越来越消耗内存的简单Java程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-25 07:01