简体   繁体   English

日期和字符串Java导致内存泄漏

[英]Memory leaking with Date and string java

I'm using the below code for having time section in my application. 我正在使用以下代码在我的应用程序中设置时间部分。 But the below code leaks memory gradually . 但是下面的代码逐渐泄漏内存 Is it because of the setText() in Jlabel ? 是因为Jlabel中的setText()吗?

Can anyone help me identifying the error? 谁能帮助我确定错误?

Also please let me know how to release the memory of Dateformat and Date in java. 另外请让我知道如何在Java中释放DateformatDate的内存。

    Thread th= new Thread(new Runnable() {

        public void run() {
            DateFormat dateformat_s2= null;
            Date date_int_s2=null;
            String date_time_s2=null;
            while(c==1) {  
                try {
                    Thread.sleep(50); 
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                dateformat_s2= new SimpleDateFormat("dd:MM:yyyy  HH:mm:ss");
                date_int_s2= new Date();
                date_time_s2 = dateformat_s2.format(date_int_s2);
                time_end_label.setText(""+date_time_s2);
                date_time_s2=null;
                dateformat_s2=null;
                date_int_s2=null;
            }
        }
    });

To monitor memory usage you need to look at the memory used after a Full GC. 要监视内存使用情况,您需要查看Full GC之后使用的内存。 Anything else will be miss leading as you will have objects which might be cleaned up but have not yet. 其他任何事情都将导致您错过所有可能被清除但尚未清除的对象。

NOTE: There is no need to be setting values to null as the GC will clean them up as you go. 注意:无需将值设置为null因为GC会在您使用时对其进行清理。 Using a local variable inside the loop also effectively discards the object on every iteration. 在循环内部使用局部变量还可以在每次迭代时有效地丢弃对象。

NOTE: You can calculate how ling it is until the next second so you can do it once per second instead of 20 times per second. 注意:您可以计算直到下一秒的延迟,因此您可以每秒执行一次,而不是每秒20次。

You could re-write you code like this 您可以这样重写代码

public static void startTimer(JLabel time_end_label) {
    Thread th = new Thread(new Runnable() {

        public void run() {
            DateFormat dateformat_s2 = new SimpleDateFormat("dd:MM:yyyy  HH:mm:ss");
            while(!Thread.currentThread().isInterrupted()) {
                Date date = new Date();
                final String date_time = dateformat_s2.format(date);
                SwingUtilities.invokeLater(new Runnable() {
                    @Override
                    public void run() {
                        time_end_label.setText(date_time);
                    }
                });
                long delay = 1000 - date.getTime() % 1000;
                try {
                    Thread.sleep(delay);
                } catch (InterruptedException e) {
                    break;
                }

            }
        }
    });
    th.setDaemon(true);
    th.start();
}

However it would be simpler to use a swing Timer as the ActionListener will be executed in the GUI event loop thread for you. 但是,使用摆动计时器会更简单,因为ActionListener将在您的GUI事件循环线程中执行。

public static void startTimer(JLabel time_end_label) {
    DateFormat dateformat= new SimpleDateFormat("dd:MM:yyyy  HH:mm:ss");
    final ActionListener listener = new ActionListener() {
        @Override
        public void actionPerformed(ActionEvent e) {
            Date date = new Date();
            final String date_time = dateformat.format(date);
            time_end_label.setText(date_time);
            int delay = (int) (1000 - date.getTime() % 1000);
            new Timer(delay, this).start();
        }
    };
    new Timer(1, listener).start();
}

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

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