问题
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