0

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?

Belphegor21
  • 454
  • 1
  • 5
  • 24
  • Maybe you just need to increase your JVM heap parameters like: `-Xms512m -Xmx1024m`. Check the following [link](http://stackoverflow.com/questions/14763079/what-are-the-xms-and-xmx-parameters-when-starting-jvms) for more info. – alexandrum Apr 03 '17 at 10:06
  • My JVM has a initial heap of 2GB. I checked that using `long heapSize = Runtime.getRuntime().totalMemory();`. – Belphegor21 Apr 03 '17 at 10:08
  • Is is possible that StringBuffer does not get allocated from heap? Maybe that's causing the error. – Belphegor21 Apr 03 '17 at 10:10
  • I am guessing it doesn't happen after a few cycle inside your while loop, what does heapFreeSize returns just before your `OutOfMemory` error occurs? – Adonis Apr 03 '17 at 10:19
  • To be able to answer your question i'll need to run the loop don't know how many times. So i'll get to it right away.. – Belphegor21 Apr 03 '17 at 10:49
  • Get the heap dump use -XX:+HeapDumpOnOutOfMemoryError as jvm argument to get it and then analyze it. for analysis see my answer (http://stackoverflow.com/questions/24888121/how-to-identify-holder-of-reference-to-object-in-java-memory-analyzer-using-heap/24895215#24895215) – Vipin Apr 07 '17 at 03:25
  • To avoid us running off in the wrong direction you should include which flavour of OutOfMemoryError you get. – piet.t Apr 12 '17 at 14:27

0 Answers0