记一次Spring Data Jpa引起的OutOfMemoryError
OOM场景
jpa分页查询
@Transactional(rollbackFor = Exception.class)
@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
修复
@Transactional(rollbackFor = Exception.class)
@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的状态可以参考
https://my.oschina.net/u/4107179/blog/3041238