我对 Java 中的 GC 有一个非常奇怪的问题。我正在运行以下代码:
while(some condition){
//do a lot of work...
logger.info("Generating resulting time series...");
Collection<MetricTimeSeries> allSeries = manager.getTimeSeries();
logger.info(String.format("Generated %,d time series! Storing in files now...", allSeries.size()));
//for (MetricTimeSeries series : allSeries) {
// just empty loop
//}
}
当我查看 JConsole 时,在每次循环迭代重新启动时,如果我手动强制 GC,我的旧 gen 堆空间会占用大约 90 MB 的大小。如果我取消注释循环,就像这样
while(some condition){
//do a lot of work...
logger.info("Generating resulting time series...");
Collection<MetricTimeSeries> allSeries = manager.getTimeSeries();
logger.info(String.format("Generated %,d time series! Storing in files now...", allSeries.size()));
for (MetricTimeSeries series : allSeries) {
// just empty loop
}
}
即使我强制它刷新,它也不会低于 550MB。根据 yourKit profiler,TimeSeries 对象可以通过主线程的本地 var(集合)访问,就在 GC 重新启动新迭代之后......而且集合很大(250K 时间序列。)...... Wyy 是这种情况发生了,我该如何“对抗”这种(不正确的?)行为?