在java的main函数里弄了一个无限循环程序,过了几天CPU满载

List<Device> list = new ArrayList<>();
        for (int i = 1; i < 123; i++) {
            list.add(new Device(i));
        }
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

        while (true) {
            String date = format.format(new Date());
            for (Device d : list) {
                d.setData();
                String message = new DeviceData(d.getIot(), d.getAmp(), d.getVolt(), date).toString();
                producer.pub("/iot="+d.getIot(),message,2);
            }

            Thread.sleep(2000);
        }

这个里面的内容就是模拟一百来台设备,每台设备的对象调用一个生成随机数据的方法并set,每隔几秒通过MQTT把message发布出去,刚开始跑的时候比较正常,过了几天以后在linux服务器下我用top查看发现CPU%达到好几百 得重启这个应用才行 有没有办法改善这种情况?

 

public class Device {
    private Integer iot;
    private Float amp;
    private Float volt;
    private Boolean isLifeCycle = false;
    private Integer lifeCycle = 60;
    private Integer interval = 10;
    private Integer time=0;

    public Device(Integer iot) {
        super();
        this.iot = iot;
    }

    public void setData() {
        Random r = new Random();
        this.setTime(this.getTime() + 1);
        if (!isLifeCycle) {
            if (this.time > this.interval) {
                if (r.nextFloat() > 0.8F) {
                    this.setTime(0);
                    this.setInterval(r.nextInt(20) + 10);
                    this.setIsLifeCycle(true);
                }
            }
            this.setAmp(r.nextFloat());
            this.setVolt(r.nextFloat());
        } else {
            if (this.time > this.lifeCycle) {
                this.setTime(0);
                this.setLifeCycle(r.nextInt(10) + 55);
                this.setIsLifeCycle(false);
            }
            this.setAmp(598f+r.nextFloat()*194f);
            this.setVolt(25.9f+r.nextFloat()*6.2f);
        }
    }
}

 

List<Device> list = new ArrayList<>();


        for (int i = 1; i < 123; i++) {


            list.add(new Device(i));


        }


        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");


 


        while (true) {


            String date = format.format(new Date());


            for (Device d : list) {


                d.setData();


                String message = new DeviceData(d.getIot(), d.getAmp(), d.getVolt(), date).toString();


                producer.pub("/iot="+d.getIot(),message,2);
                String =null;


            }


            date =null;


            Thread.sleep(2000);


        }

dump的堆栈情况,看一下是哪个占的内存大 ,不要再while(true) 每次都new,耗内存

String message = new DeviceData(d.getIot(), d.getAmp(), d.getVolt(), date).toString(); 

建议使用一个定时任务来处理你这种情况。

 如果你用Spring,可以用@Scheduled

如果没有用Spring,那么你就用Timer,CG自动回收会更有效。

new Timer().schedule(new TimerTask() {
            @Override
            public void run() {
                try {
                    //do Something
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        },0,5L * 60 * 1000);