运行以下代码会导致有关 Java 堆空间的异常。
来自 .NET 并掌握了 GC 在那里的工作方式,我想知道在尝试运行以下内容时是否需要考虑内存管理方面的问题:
public static void main(String[] args) throws NumberFormatException, ParseException, IOException {
Jedis jedis = new Jedis("<HostName>");
TimeSeriesPoints retrieved = null;
while(!finished) {
try {
finished = true;
List<String> keys = getNextFiftyKeys();
String[] cacheKeys = new String[keys.size()];
List<String> cacheResults = jedis.mget(keys.toArray(cacheKeys));
List<TimeSeries> cachedTimeSeries = new ArrayList<TimeSeries>();
for(String cacheResult : cacheResults){
try {
retrieved = TimeSeriesPoints.parseFrom(cacheResult.getBytes());
TimeSeries timeSeries = new TimeSeries(retrieved.getName(), retrieved.getPointsList());
cachedTimeSeries.add(timeSeries);
}
catch (InvalidProtocolBufferException e) {
e.printStackTrace();
}
}
long pointsCount = 0;
for(TimeSeries timeSeries : cachedTimeSeries){
pointsCount += timeSeries.points.length;
}
System.out.println("retrieved: " + cachedTimeSeries.size());
System.out.println("points:" + pointsCount);
}
}
TimeSeriesPoints.parseFrom引发异常,堆栈跟踪如下。不知道为什么。
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Unknown Source)
at java.util.Arrays.copyOf(Unknown Source)
at java.util.ArrayList.ensureCapacity(Unknown Source)
at java.util.ArrayList.add(Unknown Source)
at com.wimiro.caching.TimeSeriesProtos$TimeSeriesPoints.<init>(TimeSeriesProtos.java:115)
at com.wimiro.caching.TimeSeriesProtos$TimeSeriesPoints.<init>(TimeSeriesProtos.java:82)
at com.wimiro.caching.TimeSeriesProtos$TimeSeriesPoints$1.parsePartialFrom(TimeSeriesProtos.java:151)
at com.wimiro.caching.TimeSeriesProtos$TimeSeriesPoints$1.parsePartialFrom(TimeSeriesProtos.java:1)
at com.google.protobuf.AbstractParser.parsePartialFrom(AbstractParser.java:141)
at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:176)
at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:188)
at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:193)
at com.google.protobuf.AbstractParser.parseFrom(AbstractParser.java:49)
at com.wimiro.caching.TimeSeriesProtos$TimeSeriesPoints.parseFrom(TimeSeriesProtos.java:958)
at program.main(program.java:77)
这会导致试图读取 800 个时间序列(每个都有约 4000 个数据点)。因为在这个例子中我一次只处理 50 个时间序列,所以我预计内存占用不会显着增长。
在 .NET 中,我可以毫不费力地做到这一点。是时候向我学习一些Java了。我需要读什么?