当我使用ScheduledExecutorService运行计划任务时,我进行了此测试。但是几个小时后jvm抛出java.lang.OutOfMemoryError
并且控制台一直打印[Full GC (Ergonomics) 5099K->5099K(5632K), 0.0313980 secs]
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
public class ScheduleExecutorTest implements Runnable{
private String jobNmae = "";
public ScheduleExecutorTest(String jobNmae){
super();
this.jobNmae = jobNmae;
}
@Override
public void run(){
System.out.println("execute : "+ jobNmae);
}
public static void main(String[] args){
ScheduledExecutorService service = Executors.newScheduledThreadPool(10);
long initialDelays = 4;
long period = 2;
service.scheduleAtFixedRate(new ScheduleExecutorTest("job1") , initialDelays,period,TimeUnit.SECONDS);
long initialDelays1 = 4;
long period1 = 4;
service.scheduleWithFixedDelay( new ScheduleExecutorTest("job2"),initialDelays1,period,TimeUnit.SECONDS);
}
}
从jvm params开始:
java -Xms5m -Xmx5m -XX:+PrintGC ScheduleExecutorTest > ./output.txt
,这是output.txt的一部分
[Full GC (Ergonomics) 5096K->5008K(5632K), 0.0200085 secs]
[Full GC (Ergonomics) 5096K->5017K(5632K), 0.0230042 secs]
execute : job2
execute : job1
execute : job2
execute : job1
[Full GC (Ergonomics) 5096K->5029K(5632K), 0.0204813 secs]
[Full GC (Ergonomics) 5096K->5025K(5632K), 0.0289835 secs]
[Full GC (Ergonomics) 5096K->5047K(5632K), 0.0264593 secs]
[Full GC (Ergonomics) 5096K->5061K(5632K), 0.0284851 secs]
[Full GC (Ergonomics) 5096K->5073K(5632K), 0.0270874 secs]
[Full GC (Ergonomics) 5096K->5084K(5632K), 0.0258948 secs]
[Full GC (Ergonomics) 5096K->5088K(5632K), 0.0198362 secs]
[Full GC (Ergonomics) 5096K->5092K(5632K), 0.0209186 secs]
[Full GC (Ergonomics) 5096K->5092K(5632K), 0.0254468 secs]
[Full GC (Ergonomics) 5096K->5094K(5632K), 0.0225016 secs]
[Full GC (Ergonomics) 5096K->5096K(5632K), 0.0194505 secs]
[Full GC (Ergonomics) 5099K->5099K(5632K), 0.0176883 secs]
[Full GC (Ergonomics) 5099K->5097K(5632K), 0.0205154 secs]
[Full GC (Ergonomics) 5099K->5098K(5632K), 0.0206259 secs]
我是java的新手,想知道使用ScheduledExecutorService的正确方法是什么。提前谢谢