[Bug libgcj/16134] New: Memory leak in String.getBytes()

hannes at helma dot at gcc-bugzilla@gcc.gnu.org
Tue Jun 22 10:23:00 GMT 2004


Looks like there is a memory leak in java.lang.String.getBytes() in libgcj
3.3.3. The following code keeps growing continuously in memory:

//
// Test code for String.getBytes() memory leak
//     
    
public class StringBytesLeak {

    public static void main(String[] args) {
        while (true) {
            for (int i=0; i<10000; i++) {
                // leaks memory
                "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx".getBytes();
                // using our own bogus implementation fixes the problem
                // getBytes("xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx");
            }
            try {
                Thread.sleep(500);
            } catch (InterruptedException x) {
                System.exit(0);
            }
        }
    }

    // simplistic implementation of String.getBytes() that doesn't leak
    static byte[] getBytes(String str) {
        int length = str.length();
        byte[] bytes = new byte[length];
        for (int i=0; i<length; i++) 
            bytes[i] = (byte) str.charAt(i);
        return bytes;
    }

}

-- 
           Summary: Memory leak in String.getBytes()
           Product: gcc
           Version: 3.3.3
            Status: UNCONFIRMED
          Severity: normal
          Priority: P2
         Component: libgcj
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: hannes at helma dot at
                CC: gcc-bugs at gcc dot gnu dot org,java-prs at gcc dot gnu
                    dot org
 GCC build triplet: i686-suse-linux
  GCC host triplet: i686-suse-linux
GCC target triplet: i686-suse-linux


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



More information about the Java-prs mailing list