From d78f2ee3f2bd9ce38d77d70c0893e16b9d0f8df4 Mon Sep 17 00:00:00 2001 From: Nico Kruber Date: Mon, 28 Jan 2013 20:01:14 +0100 Subject: jinterface: fix a memory leak after the first try to compress the value with a fixed buffer size, the deflater must be closed so that memory can be (instantly) reused --- .../java_src/com/ericsson/otp/erlang/OtpOutputStream.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) (limited to 'lib/jinterface/java_src/com/ericsson/otp/erlang') diff --git a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpOutputStream.java b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpOutputStream.java index 1512f9f2aa..884a0cd21c 100644 --- a/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpOutputStream.java +++ b/lib/jinterface/java_src/com/ericsson/otp/erlang/OtpOutputStream.java @@ -26,6 +26,7 @@ import java.math.BigDecimal; import java.math.BigInteger; import java.text.DecimalFormat; import java.util.Arrays; +import java.util.zip.Deflater; /** * Provides a stream for encoding Erlang terms to external format, for @@ -824,14 +825,18 @@ public class OtpOutputStream extends ByteArrayOutputStream { // -> if compression uses more, use the uncompressed term! int destCount = startCount + oos.size(); this.fixedSize = destCount; + Deflater def = new Deflater(); final java.util.zip.DeflaterOutputStream dos = new java.util.zip.DeflaterOutputStream( - this); + this, def); try { write1(OtpExternal.compressedTag); write4BE(oos.size()); oos.writeTo(dos); dos.close(); // note: closes this, too! } catch (final IllegalArgumentException e) { + // discard further un-compressed data + // -> if not called, there may be memory leaks! + def.end(); // could not make the value smaller than originally // -> reset to starting count, write uncompressed super.count = startCount; -- cgit v1.2.3