Invoking java.util.ResourceBundle.getBundle(String) leaks some native memory. 
A small sample program is attached.  If you run it, you will see that the
process' virual memory size keeps growing while Java heap consumption stays at
a same level after some iteration.

The problem is in gnu.gcj.runtime.StackTrace.fillInStackTrace (a native code)
invoked through gnu.gcj.runtime.StackTrace.classAt through
java.lang.Class.forName through java.util.ResourceBundle.tryBundle.  When the
native method fillInStackTrace is invoked by a method other than its
constructor, a native memory allocated through _Jv_Malloc and stored in addrs
is discarded without _Jv_Free'ed.

I exprerienced this problem on Windows (MinGW) but I belive it is common to
other platforms.

A suggested fix is attached:

--- libjava/gnu/gcj/runtime/natStackTrace.orig.cc       2003-10-02
16:10:34.000000000 +0900
+++ libjava/gnu/gcj/runtime/natStackTrace.cc    2006-02-17 00:01:52.529572700
+0900
@@ -85,6 +85,8 @@
   else
     frame = NULL;

+  if (addrs != NULL)
+    _Jv_Free (addrs);
   addrs = reinterpret_cast<gnu::gcj::RawData *> (frame);
 #else // HAVE_BACKTRACE
   (void)maxlen;

---

A sample program to reproduce the problem follows:

import java.util.ResourceBundle;
import java.util.MissingResourceException;

public class Leak {
    public static void main(String[] args) {
        final Runtime r = Runtime.getRuntime();
        for (;;) {
            for (int i = 0; i < 10000; i++) {
                try {
                    ResourceBundle.getBundle("nothing");
                } catch (MissingResourceException e) {
                }
            }
            System.out.println(r.freeMemory() + "/" + r.totalMemory());
        }
    }
}


-- 
           Summary: Native Momory Leak in ResourceBundle
           Product: gcc
           Version: 3.4.5
            Status: UNCONFIRMED
          Severity: major
          Priority: P3
         Component: libgcj
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: fexx at fexx dot org
 GCC build triplet: mingw32
  GCC host triplet: mingw32
GCC target triplet: mingw32


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=26351

Reply via email to