OutOfMemory exception in a lot of memory

↘锁芯ラ 提交于 2020-01-22 02:15:08

问题


Sorry for the bad question name but i couldn't find anything better.

I have some code

long heapFreeSize = Runtime.getRuntime().freeMemory();
URL url = new URL("http://example.com");
URLConnection myUrlConnection = url.openConnection();
GZIPInputStream gZIPInputStream = new GZIPInputStream(myUrlConnection.getInputStream());
StringBuffer decompressedStringBuffer = new StringBuffer();
int bytes_read;
int bufferSize;
heapFreeSize = Runtime.getRuntime().freeMemory();
while ((bytes_read = gZIPInputStream.read(buffer)) > 0) {

    String part = new String(buffer, 0 ,bytes_read, "UTF-8");
    heapFreeSize = Runtime.getRuntime().freeMemory();
    decompressedStringBuffer.append(part);
    bufferSize = decompressedStringBuffer.length();
    heapFreeSize = Runtime.getRuntime().freeMemory();
}

The URL in the code returns a gzip file which i decompress and store in a string for further processing. I have been working with a smaller file to test my logic but when i moved to the full file I get an OutOfMemory Exception. If i download the file to my pc and extract it the text file extracted is approximately 140MB in size. Using heapFreeSize I can see that my heap has 800+ MB of free memory.

I get the error on decompressedStringBuffer.append(part); line.

Why am i getting a OutOfMemory error when there is still so much memory available?

来源:https://stackoverflow.com/questions/43181678/outofmemory-exception-in-a-lot-of-memory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!