Bug 42823 - tcp/ip sockets read/write operations causes memory leak
Summary: tcp/ip sockets read/write operations causes memory leak
Alias: None
Product: classpath
Classification: Unclassified
Component: classpath (show other bugs)
Version: 0.98
: P3 major
Target Milestone: 0.99
Assignee: Not yet assigned to anyone
Depends on:
Reported: 2010-01-21 11:35 UTC by javadev
Modified: 2011-03-31 00:33 UTC (History)
5 users (show)

See Also:
Known to work:
Known to fail:
Last reconfirmed:

Patch for leaks in VMInetAddress.c (328 bytes, patch)
2011-03-30 11:11 UTC, Gert Brettlecker
Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description javadev 2010-01-21 11:35:41 UTC
I have a device which has a linux embedded system. I have cacao 0.99.4 jvm + classpath 0.98. A swing application is running on the box and after few weeks it raises Out of Memory. I suspect there is a memory leak on sockets read/write methods. Because:
I created a dummy Client Server application that only transmits raw bytes. If the server is on the PC and the client runs on the device continuously, when I investigate the memory on the device I can see that is growing (7.1 MB RAM, 7.2,.., 7.9 MB RAM) in less than one hour.
Comment 1 Martin Winter 2011-01-13 13:39:01 UTC
I see a very similar behaviour on a system using classpath 0.98 and jamvm 1.5.4. I assume it is related to opening and closing sockets, becauce with an continuously open socket I can do I/O for weeks, but when opening and closing it all the time I run out of memory in a day, i.e. about 20000 socket open using (roughly) 20MByte of memory.
Comment 2 Gert Brettlecker 2011-03-30 11:09:07 UTC

I had similiar problems and found two leaks in VMInetAddress.c which can be fixed with:

--- classpath-0.98/native/jni/java-net/java_net_VMInetAddress.c
+++ classpath-0.98/native/jni/java-net/java_net_VMInetAddress.c
@@ -180,6 +180,7 @@
   /* Resolve the address and return the name */
   result = cpnet_getHostByAddr (env, addr, hostname, sizeof (hostname));
+  cpnet_freeAddress (env, addr);
   if (result != CPNATIVE_OK)
       JCL_ThrowException (env, UNKNOWN_HOST_EXCEPTION,
@@ -330,6 +331,7 @@
   result = cpnet_aton (env, hostname, &address);
+  (*env)->ReleaseStringUTFChars (env, host, hostname);
   if (result != CPNATIVE_OK)
       JCL_ThrowException (env, UNKNOWN_HOST_EXCEPTION, "Internal Error");

I also add this as proposed patch.

Comment 3 Gert Brettlecker 2011-03-30 11:11:26 UTC
Created attachment 23817 [details]
Patch for leaks in VMInetAddress.c

Patch for leaks in VMInetAddress.c
Comment 4 mscott 2011-03-30 21:01:06 UTC
We are experiencing similiar memory leaks, not on open/close operations but on read/write. An average of 400 bytes per message when using sockets to transmit data between .Net client and Java service both running in Win CE. 

GNU Classpath v0.97.1
JVM      MySaifu 0.4.8
OS       WinCE 6.0
Platform ARM4i

Each request to the java service is at least 2 packets in length (We needed to implemnet our own flush() by padding messages with 1493 bytes to ensure that the last bytes of our message are pushed through the layers)

We do not close connections between requests
When monitoring memory usage in the Win CE environment with a process monitor/task manager we can see over time increased memory consumption.

Calcualtion of memory usage over ten, hundreds or thousands of messages shows aproximatelyt 400 bytes per message!
Comment 5 Andrew John Hughes 2011-03-31 00:33:23 UTC
I've committed the patch:

CVSROOT:        /sources/classpath
Module name:    classpath
Changes by:     Andrew John Hughes <gnu_andrew> 11/03/31 00:30:29

Modified files:
       .              : ChangeLog
       native/jni/java-net: java_net_VMInetAddress.c

Log message:
       PR classpath/42823: Remove memory leaks in native code of java.net.VMInetAddress.

       2011-03-30  Gert Brettlecker  <gert.brettlecker@ergon.ch>

               PR classpath/42823
               * native/jni/java-net/java_net_VMInetAddress.c,
               Free address structure after use.
               Release UTF string after use.


I'll try and get a new release out soon.
Comment 6 Andrew John Hughes 2011-03-31 00:33:42 UTC
Mark as in 0.99.