Java - GUI 时钟使用大量 RAM?

Arm*_*man 0 java timer

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

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();
Run Code Online (Sandbox Code Playgroud)

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

Mad*_*mer 6

  1. 将更新数量减少到所需的最低限度
  2. 尽可能减少临时对象的数量
  3. 确保对 UI 的所有更新都是在主 UI 线程(Swing 的事件调度线程)的上下文中进行的

看一眼:

例如...

钟

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

        }

    }

}
Run Code Online (Sandbox Code Playgroud)

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