Java - 使用大量RAM的GUI时钟? [英] Java - GUI clock using large amounts of RAM?

查看:153
本文介绍了Java - 使用大量RAM的GUI时钟?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我为Java中的桌面小部件制作了一个小时钟(该小部件还包括许多其他功能)。我检查了任务管理器中的应用程序RAM使用情况,看看时钟是否使用了700多MB的RAM。我禁用了时钟,RAM使用率下降到大约60 MB。这是时钟代码:

I made a little clock for a desktop widget in Java(the widget includes many other features as well). I checked the applications RAM usage in task manager to see that the clock was using 700+ MB of RAM. I disabled the clock and the RAM usage went down to about 60 MB. Here is the clocks code:

final int timeRun = 0;
    new Thread()
    {
        public void run()
        {
            while(timeRun == 0)
            {
                Calendar cal = new GregorianCalendar();
                int hour = cal.get(Calendar.HOUR);
                int min = cal.get(Calendar.MINUTE);
                int sec = cal.get(Calendar.SECOND);
                int AM_PM = cal.get(Calendar.AM_PM);

                String day_night = "";

                if (AM_PM == 1){
                    day_night = "PM";
                }else{
                    day_night = "AM";
                }

                String time = hour + ":" + min + ":" + sec + " " + day_night;
                Clock.setText(time);
            }
        }
    }.start();

为什么要使用这么多内存?我该如何解决?

Why is it using so much RAM? How could I fix it?

推荐答案


  1. 将更新次数减少到所需的最低数量

  2. 尽可能减少临时对象的数量

  3. 确保对UI的所有更新都是在主UI线程的上下文中进行的(事件)为Swing调度线程)

看看:

  • Concurrency in Swing
  • How to use Swing Timers

例如......

import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ClockMeBaby {

    public static void main(String[] args) {
        new ClockMeBaby();
    }

    public ClockMeBaby() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                JFrame frame = new JFrame("Testing");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.add(new TestPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public static class TestPane extends JPanel {

        protected static final DateFormat CLOCK_FORMAT = new SimpleDateFormat("hh:mm:ss a");
        private JLabel clock;

        public TestPane() {
            setLayout(new GridBagLayout());
            clock = new JLabel("...");
            clock.setFont(clock.getFont().deriveFont(Font.BOLD, 64f));
            add(clock);
            updateClock();

            Timer timer = new Timer(500, new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    updateClock();
                }
            });
            timer.start();
        }

        protected void updateClock() {

            clock.setText(CLOCK_FORMAT.format(System.currentTimeMillis()));

        }

    }

}

SwingTimer 使用 500 毫秒延迟的原因是为了确保我们保持同步,否则你的时钟可能会与UI的其余部分不同步,因为你错过了第二个边界。如果这对您不重要,您可以 1000 毫秒延迟

The reason the SwingTimer uses a 500 millisecond delay is to ensure we remain in sync, otherwise your clock might update "out of sync" with the rest of the UI because you've missed a second boundry. If this is not important to you, you could us 1000 millisecond delay instead

这篇关于Java - 使用大量RAM的GUI时钟?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆