我编写了一个应用程序(Springboot + Data JPA + Data Rest),该应用程序在加载时不断向我抛出OutOfMemoryException。我可以跳过在应用程序启动时运行的代码,但随后可能会在以后发生异常。最好向您显示在应用程序启动时会发生什么,因为它实际上非常简单,不会引起任何问题恕我直言:
@SpringBootApplication
@EnableAsync
@EnableJpaAuditing
public class ScraperApplication {
public static void main(String[] args) {
SpringApplication.run(ScraperApplication.class, args);
}
}
@Component
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class DefaultDataLoader {
private final @NonNull LuceneService luceneService;
@Transactional
@EventListener(ApplicationReadyEvent.class)
public void load() {
luceneService.reindexData();
}
}
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class LuceneService {
private static final Log LOG = LogFactory.getLog(LuceneService.class);
private final @NonNull TrainingRepo trainingRepo;
private final @NonNull EntityManager entityManager;
public void reindexData() {
LOG.info("Reindexing triggered");
FullTextEntityManager fullTextEntityManager = Search.getFullTextEntityManager(entityManager);
fullTextEntityManager.purgeAll(Training.class);
LOG.info("Index purged");
int page = 0;
int size = 100;
boolean morePages = true;
Page<Training> pageData;
while (morePages) {
pageData = trainingRepo.findAll(PageRequest.of(page, size));
LOG.info("Loading page " + (page + 1) + "/" + pageData.getTotalPages());
pageData.getContent().stream().forEach(t -> fullTextEntityManager.index(t));
fullTextEntityManager.flushToIndexes(); // flush regularly to keep memory footprint low
morePages = pageData.getTotalPages() > ++page;
}
fullTextEntityManager.flushToIndexes();
LOG.info("Index flushed");
}
}
您可以看到我正在做的事情是清除索引,以分页的方式(一次100个)从TrainingRepo中读取所有培训,并将它们写入索引。实际上没有太多的事情。在“清除索引”消息后的几分钟,我得到了-仅此:
java.lang.OutOfMemoryError: Java heap space
我让JVM编写了一个堆转储,并将其装入Eclipse Memory Analyzer,并获得了完整的堆栈跟踪:https://gist.github.com/mathias-ewald/2fddb9762427374bb04d332bd0b6b499
我也稍微看了一下报告,但是我需要帮助解释此信息,这就是为什么我附加了Eclipse Memory Analyzer的一些屏幕截图的原因。
我相信这与您的FullTextEntityManager找不到足够的内存有关。您必须配置queryPlanCache。通过此线程了解如何Stackoverflow和this one too.