我已经写了一个程序,每天24小时后自称。我在服务器上有350个实时运行设备。我需要每天自动更新设备的报告。
@POST
@Path("/UpdateDevicesStats")
public static void updateAllLiveDevicesStats(){
long period = 1*24*60*60*1000;
long delay = 0;
new Timer().schedule(new TimerTask() {
@Override
public void run() {
try {
ArrayList<Device> devices = DeviceBuilder.getCurrentLiveDevices();
try {
for(int i=0;i<devices.size();i++){
System.out.println("Updating Device = "+devices.get(i).getId());
// This below line is custom code line.
// This is not any in-built library.
SummaryBuilder.updateDBStats(devices.get(i).getId());
Thread.sleep(1*60*1000);
}
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}, delay, period);
}
当我运行此代码时。它进行了4到5个小时的很好,但此后向我展示了这一点:
failed: error='Cannot allocate memory' (errno=12)
There is insufficient memory for the Java Runtime Environment to continue. Native memory allocation (mmap) failed to map 12288 bytes for committing reserved memory.
ignoring option PermSize=500m; support was removed in 8.0
ignoring option MaxPermSize=256m; support was removed in 8.0
Setting property 'maxSpareThreads' to '75' did not find a matching property.
有人知道其他更好的方法吗?
我通过更新统计数据和 Thread.sleep();
之后以及其他一些后的语句泄漏的语句来解决Runtime.getRuntime().gc();
解决此问题。现在,它没有遇到任何Java堆错误。谢谢@joopeggen,@javajd。