记一次Spring Data Jpa引起的OutOfMemoryError
OOM场景
使用框架
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
<version>2.1.0.RELEASE</version>
</dependency>
设置jvm参数-Xms200m -Xmx200m,方便快速出现OOM
jpa分页查询
@Override
public Resource testJpaOom() {
int page = 0;
Page<OrderDO> orderDOPage = orderDao
.findAll(PageRequest.of(page, OrderConstants.DEFAULT_SIZE));
int totalPages = orderDOPage.getTotalPages();
for (; page < totalPages; page++) {
log.info("start totalPages:[{}],page:[{}]", totalPages, page);
orderDao.findAll(PageRequest.of(page, OrderConstants.DEFAULT_SIZE));
log.info("end totalPages:[{}],page:[{}]", totalPages, page);
}
return null;
}
OOM异常
Exception in thread "http-nio-8086-exec-1" java.lang.OutOfMemoryError: GC overhead limit exceeded
修复
@Override
public Resource testJpaOom() {
int page = 0;
Page<OrderDO> orderDOPage = orderDao
.findAll(PageRequest.of(page, OrderConstants.DEFAULT_SIZE));
int totalPages = orderDOPage.getTotalPages();
for (; page < totalPages; page++) {
log.info("start totalPages:[{}],page:[{}]", totalPages, page);
orderDao.findAll(PageRequest.of(page, OrderConstants.DEFAULT_SIZE));
log.info("end totalPages:[{}],page:[{}]", totalPages, page);
// 清除持久上下文环境
entityManager.flush();
entityManager.clear();
}
return null;
}
总结
托管状态下的bean不会被垃圾回收。关于jpa下bean的状态可以参考