2012년 4월 18일 수요일

[TechNote] Improper closing of deflater object results in native memory exhaustion

Improper closing of deflater object results in native memory exhaustion

Problem(Abstract)

If a deflater object is not explicitly closed and ended, a native memory leak occurs. This leak can result in OutOfMemoryError's and/or Java™ Virtual Machine (JVM) crashes.

Cause

Not explicitly closing and ending a deflater object (such as, DeflaterOutputStream, GZIPOutputStream or ZipOutputStream) causes a native memory leak. This native leak can result in OutOfMemoryError's in the WebSphere Application Server logs as well as JVM hangs and crashes. The following error in the WebSphere Application Server SystemOut.log orSystemErr.log files is a potential indicator of a native issue caused by this type of deflater leak:
java.lang.OutOfMemoryError: ZIP002:OutOfMemoryError, MEM_ERROR in deflate_init2
   at java.util.zip.Deflater.init(Native Method)
   at java.util.zip.Deflater.<init>(Deflater.java:135)
   at java.util.zip.ZipOutputStream.<init>(ZipOutputStream.java:84)
   ...

Resolving the problem

To avoid the leak, the application programmer should insure that all deflater objects are explicitly closed and ended.
The following is a code for properly ending a deflater:

try {
   Deflater zip = new Deflater(level);
   DeflaterOutputStream defStream = new DeflaterOutputStream(out);
} 
finally {
   defStream.close();
   zip.end();
}

If the application is not explicitly creating a Deflater object, Java creates a default Deflater. In this case, the application need only close the DeflaterOutputStream and this will end the default Deflater object. For example:

try {
   /// create default Deflater
   DeflaterOutputStream defStream = new DeflaterOutputStream(out);
}
finally {
   /// end the Deflater object.  
   defStream.close();
}

댓글 없음:

댓글 쓰기