- 异常描述
- 环境描述
- 问题分析
- 问题复现
- 问题解决
- 相关代码
- 总结
容器异常
Exception in thread \"Timer-0\" Exception in thread \"http-nio-30021-Acceptor-0\" Exception in thread \"http-nio-30021-exec-6\" java.lang.OutOfMemoryError: GC overhead limit exceeded
java.lang.OutOfMemoryError: Java heap space
Exception in thread \"logback-7\" java.lang.OutOfMemoryError: GC overhead limit exceeded
java.lang.OutOfMemoryError: GC overhead limit exceeded
Exception in thread \"http-nio-30021-exec-8\" java.lang.OutOfMemoryError: GC overhead limit exceeded
2022-04-21 04:13:41,759 WARN (SqlExceptionHelper.java:129)- SQL Error: 0, SQLState: null
2022-04-21 04:13:41,759 ERROR (SqlExceptionHelper.java:131)- Error
2022-04-21 04:13:41,784 INFO (TrolleeController.java:43)- ----------从后台获取渠道信息----------
2022-04-21 04:13:41,878 ERROR (DirectJDKLog.java:182)- Servlet.service() for servlet [dispatcherServlet] in context with path [/Sys-Map-Server] threw exception [Handler dispatch failed; nested exception is java.lang.OutOfMemoryError: GC overhead limit exceeded] with root cause
java.lang.OutOfMemoryError: GC overhead limit exceeded
2022-04-21 04:13:41,904 INFO (StartTask.java:45)- ----------定时任务:删除设备位置上报记录失败----------
环境描述
问题分析采用Docker容器构建Tomcat容器
Jvm配置:java -Xms1024m -Xmx1280m -jar xxxxxxxx.jar &
- 上述异常为Docker容器打印日志,业务服务并未打印相关异常错误。由此分析是业务服务导致的内存溢出致使业务服务异常停止。
- jvm配置分析应该也不会是正常运行导致的内存不足。
- 全局搜索服务中涉及到while循环的内容(并未发现相关问题)。
- 详细分析错误log发现错误发生的时间集中在每个小时的13-14分钟之间,由此将问题定位至定时任务模块。发现每次在定时任务删除设备位置记录的时候报错(见问题复现)。
- 问题中发现服务在执行SQL的过程中等待时间过久从而导致该线程一直占用资源从而导致内存溢出。
由此可以初步判断服务内存溢出的原因为SQL执行过久。
问题复现- 配置IDEA内存:
更改定时任务时间,很快便复现了问题。
根据上述可得出是因为SQL执行过久导致的问题。解决办法如下2中。
- 根据具体的业务逻辑更改SQL语句,降低SQL执行的时间。
- 设置业务超时时间,超市抛出异常,停止线程,释放占用的服务器资源。
- 设置业务超时时间
/**
* 设置相关业务逻辑实现的超时时间
*
* @param task 任务(业务逻辑)
* @param timeout 超时时间(单位:s)
* @author stefanie
* @date 2022/4/24 11:14
**/
public <T> void startTimer(Callable<T> task, long timeout) throws TimeoutException {
ExecutorService executorService = Executors.newSingleThreadExecutor();
FutureTask<T> futureTask = (FutureTask<T>) executorService.submit(task);
executorService.execute(futureTask);
try {
futureTask.get(timeout, TimeUnit.SECONDS);
} catch (TimeoutException e) {
throw e;
} catch (Exception e) {
throw new TimeoutException();
} finally {
executorService.shutdown();
}
}
- 业务调用
public void test() {
try {
TimeOutUtil.startTimer(new Callable<Boolean>() {
@Override
public Boolean call() throws Exception {
//TODO 业务逻辑
return false;
}
}, 10);
} catch (TimeoutException e) {
e.printStackTrace();
log.info("服务运行超时");
}
}
总结
- 在服务部署之前做内存测试,设置开发工具的jvm,确保问题能够在开发/测试阶段发现。
- 在编写定时任务的时候注意留意任务执行时间,如果时间过久设置超时时间及处理。
- 对相关SQL语句优化。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)