I am getting a java.lang.OutOfMemoryError: Java heap space when using GZIPInputStream. The Java process runs good for some time but after a while full ups the memory. I guess there is some references that are not taken care by the GC but can really find where the problem in the code could be. I already increase the memory of the process to 3 GB but for sure after a while will full up that memory too. Is really progressive and no matter the memory size. Does any one have an idea how I could improve my code to prevent memory leakage?
public byte[] uncompress(byte[] msg) {
byte[] buffer = new byte[4 * 1024];
int length;
try (GZIPInputStream gzis = new GZIPInputStream(new ByteArrayInputStream(msg));
BufferedInputStream bis = new BufferedInputStream(gzis);
ByteArrayOutputStream baos = new ByteArrayOutputStream()
) {
while ((length = bis.read(buffer)) >= 0) {
baos.write(buffer, 0, length);
}
final byte[] result = baos.toByteArray();
return result;
} catch (Exception e) {
}
}