调整JFrame的大小会增加大量内存

时间:2013-05-12 07:19:10

标签: java swing memory jframe

我正在创建一个简单的JFrame窗口并检查Windows任务管理器的内存使用情况,当我尝试使用鼠标指针调整JFrame的大小而不是最大按钮时,它会从51,000k到400,000k添加大量内存而且从不下降但是当我使用最大按钮时它甚至没有增加内存。

是什么导致它添加大量内存并且它永远不会停止?

Image bg = new ImageIcon(getClass().getResource("circle.png")).getImage();
    JFrame jf = new JFrame("MySqlConnections");
    jf.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    jf.setSize(400,400);
    jf.setIconImage(bg);
    new ConnectMysql(jf);
    jf.setLocationRelativeTo(null);
    jf.setPreferredSize(new Dimension(xFrame, yFrame)); 
    jf.setMinimumSize(jf.getPreferredSize());
    jf.setVisible(true);


public static void main(String[] args) {
    new MainFrame();
     /* Total number of processors or cores available to the JVM */
    System.out.println("Available processors (cores): " + 
        Runtime.getRuntime().availableProcessors());

    /* Total amount of free memory available to the JVM */
    System.out.println("Free memory (bytes): " + 
        Runtime.getRuntime().freeMemory());

    /* This will return Long.MAX_VALUE if there is no preset limit */
    long maxMemory = Runtime.getRuntime().maxMemory();
    /* Maximum amount of memory the JVM will attempt to use */
    System.out.println("Maximum memory (bytes): " + 
        (maxMemory == Long.MAX_VALUE ? "no limit" : maxMemory));

    /* Total memory currently in use by the JVM */
    System.out.println("Total memory (bytes): " + 
        Runtime.getRuntime().totalMemory());

    /* Get a list of all filesystem roots on this system */
    File[] roots = File.listRoots();

    /* For each filesystem root, print some info */
    for (File root : roots) {
      System.out.println("File system root: " + root.getAbsolutePath());
      System.out.println("Total space (bytes): " + root.getTotalSpace());
      System.out.println("Free space (bytes): " + root.getFreeSpace());
      System.out.println("Usable space (bytes): " + root.getUsableSpace());
    }

}

1 个答案:

答案 0 :(得分:1)

当您逐步调整大小时,它可能会添加内存,它需要一次又一次地重新绘制整个帧。为什么它没有下降,因为它依赖于垃圾收集器,只要有空间可以释放一些内存,它就不会立即