2022-10-05 01:55:02.970|ERROR|roosterScheduledFactory_Worker-7||||org.quartz.core.ErrorLogger.schedulerError[2401]|Job (ROOSTER_JOBGROUP.SendCollectionJob threw an exception.
org.quartz.SchedulerException: Job threw an unhandled exception.
at org.quartz.core.JobRunShell.run(JobRunShell.java:213)
at org.quartz.simpl.SimpleThreadPool$WorkerThread.run(SimpleThreadPool.java:573)
Caused by: java.lang.OutOfMemoryError: unable to create new native thread
at java.lang.Thread.start0(Native Method)
at java.lang.Thread.start(Thread.java:714)
at java.util.concurrent.ThreadPoolExecutor.addWorker(ThreadPoolExecutor.java:950)
at java.util.concurrent.ThreadPoolExecutor.execute(ThreadPoolExecutor.java:1357)
1. 序言
国庆期间 线上出现了java.lang.OutOfMemoryError :unable to create a new native thread错误,
unable to create new thread类型的OOM错误,出现这种问题呢 ,无非在于两种情况,
1.系统内存耗尽,无法为新线程分配内存
2.创建线程数超过了操作系统的限制(linux中可以通过ulimit -a查看系统的最大线程数)
3.代码本身有问题,创建了太多不必要的线程
2.排查
经过运维同事的反馈呢,项目分了8个G的内存,目前只占用了两个G,好,那么排除第一种情况
运维同事继续反馈说 创建了上万个线程 超过了操作系统允许创建的最大线程数,我突然想起了前几天上线的做的一个催收定时器的优化,用到了线程池多线程去跑任务,我们的定时器每五分钟执行一次,去跟踪代码,发现在定时器的方法里创建的线程池,并且还没有shutdown(),所以导致每次执行都会创建一个线程池 ,找到问题的原因后,有两种解决办法:
方法a:将创建的线程池作为全局变量,放在外面,也无需在执行完后进行shutdown()
方法b:线程池还是作为局部变量,只不过是每次执行完后,进行shutdown();
总结:那么这两个方法哪个更好呢,个人选用了方法a:因为我们的定时器五分钟执行一次,需要循环利用这个线程池,每次创建关闭的话会损耗一定的性能)
3.参考文档:
解决OutOfMemoryError: unable to create new native thread问题_iechenyb_的博客-CSDN博客
java.lang.OutOfMemoryError: unable to create new native thread原因排查_晓之木初的博客-CSDN博客