[Mono-bugs] [Bug 60835][Nor] New - memory leak in System.Net.Socket ?

bugzilla-daemon@bugzilla.ximian.com bugzilla-daemon@bugzilla.ximian.com
Mon, 28 Jun 2004 14:41:53 -0400 (EDT)


Please do not reply to this email- if you want to comment on the bug, go to the
URL shown below and enter your comments there.

Changed by ralinx@gmail.com.

http://bugzilla.ximian.com/show_bug.cgi?id=60835

--- shadow/60835	2004-06-28 14:41:53.000000000 -0400
+++ shadow/60835.tmp.18795	2004-06-28 14:41:53.000000000 -0400
@@ -0,0 +1,187 @@
+Bug#: 60835
+Product: Mono: Class Libraries
+Version: unspecified
+OS: 
+OS Details: Fedora Core 2
+Status: NEW   
+Resolution: 
+Severity: 
+Priority: Normal
+Component: CORLIB
+AssignedTo: mono-bugs@ximian.com                            
+ReportedBy: ralinx@gmail.com               
+QAContact: mono-bugs@ximian.com
+TargetMilestone: ---
+URL: 
+Cc: 
+Summary: memory leak in System.Net.Socket ?
+
+Description of Problem: 
+ 
+i've noticed that receiving data from a socket in a loop increases memory  
+usage slowly.  It's like the entire stream of data is being kept in 
+memory. Once you read data from a socket, and the byte array which 
+contains the data goes out of scope, shouldn't that data be removed from 
+the memory? 
+ 
+I wrote a small test application which shows what i'm talking about... it 
+will connect to the Freenode irc server and join some popular channels 
+and then it will just read the incoming data in a loop.  the incoming 
+data is just being read from the socket, it goes out of scope shortly 
+after calling Socket.Receive yet memory usage keeps increasing.  
+Obviously, in this case the problem isn't very big because it's only one 
+connection.  But it's noticable if you keep an eye on the memory usage 
+and as you can imagine, this problem becomes quite a pain in the neck 
+once you start connecting to more than 10 high traffic irc servers. 
+ 
+So like i said, it looks like the socket is keeping the entire stream in  
+memory or something like that... but shouldn't that data be removed from  
+memory once you've read it from the socket?  With MS.NET this doesn't 
+happen. 
+ 
+ 
+Steps to reproduce the problem: 
+1. execute the following code and keep an eye on the memory usage 
+ 
+using System; 
+using System.Net; 
+using System.Net.Sockets; 
+using System.Threading; 
+using System.Text; 
+ 
+class Connection 
+{ 
+        private Socket m_sckSocket; 
+        private long m_TotalBytesRead; 
+         
+        public Connection() 
+        { 
+                m_TotalBytesRead = 0; 
+        } 
+                 
+        public void Connect() 
+        { 
+                IPEndPoint ipeEndpoint; 
+                IPHostEntry iphHostEntry; 
+                IPAddress ipaAddress = null; 
+ 
+                m_sckSocket = new 
+Socket(AddressFamily.InterNetwork,SocketType.Stream,ProtocolType.Tcp); 
+                try 
+                { 
+                        iphHostEntry = Dns.Resolve("irc.freenode.net"); 
+                        if ( iphHostEntry.AddressList.Length > 0 ) 
+                        { 
+                                ipaAddress = iphHostEntry.AddressList[0]; 
+                                ipeEndpoint = new 
+IPEndPoint(ipaAddress,6667); 
+                                m_sckSocket.Connect(ipeEndpoint); 
+ 
+                                if ( m_sckSocket.Connected ) 
+                                { 
+                                        // complete the connection 
+attempt 
+                                        Send("NICK abcdefg"); 
+                                        Send("USER abcdefg 8 
+* :abcdefg"); 
+                                        // join some popular channels to 
+get some incoming traffic 
+                                        Send("JOIN #gentoo"); 
+                                        Send("JOIN #debian"); 
+                                        Send("JOIN #php"); 
+                                        Send("JOIN #fedora"); 
+                                        Send("JOIN #linux"); 
+                                        Send("JOIN #python"); 
+                                        Send("JOIN #C"); 
+                                        Send("JOIN #perl"); 
+                                        Send("JOIN #c++"); 
+                                        Send("JOIN #slackware"); 
+                                        Send("JOIN #freebsd"); 
+                                } 
+                        }                                
+                } 
+                catch (Exception ex) 
+                { 
+                        throw ex; 
+                }                        
+        } 
+         
+        public void GetData() 
+        { 
+                int intBytesRead; 
+                byte[] abytData; 
+ 
+                try 
+                {                
+                        if ( m_sckSocket.Available > 0 ) 
+                        { 
+                                abytData = new byte[128]; 
+                                intBytesRead = 
+m_sckSocket.Receive(abytData); 
+                                m_TotalBytesRead += intBytesRead; 
+                                Console.WriteLine( "Total bytes read: " + 
+m_TotalBytesRead.ToString() ); 
+                        } 
+                } 
+                catch (Exception ex) 
+                { 
+                        throw ex; 
+                }                
+        } 
+         
+        private void Send(string aMessage) 
+        { 
+                try 
+                { 
+                        m_sckSocket.Send(Encoding.ASCII.GetBytes(aMessage 
++ "\r\n")); 
+                } 
+                catch (Exception ex) 
+                { 
+                        throw ex; 
+                } 
+        } 
+} 
+ 
+ 
+ 
+class MainClass 
+{ 
+        public static void Main(string[] args) 
+        { 
+                Connection myConnection = new Connection(); 
+                 
+                try 
+                { 
+                        myConnection.Connect(); 
+                         
+                        while (true) 
+                        { 
+                                myConnection.GetData(); 
+                                Thread.Sleep(50); 
+                        } 
+                } 
+                catch (Exception ex) 
+                { 
+                        Console.WriteLine(ex.ToString()); 
+                } 
+        } 
+} 
+ 
+ 
+Actual Results: 
+Memory usage keeps increasing 
+ 
+ 
+Expected Results: 
+Memory usage should not increase because the data that's being read from 
+the socket is not being kept in memory by the application 
+ 
+ 
+How often does this happen?  
+Every time... this behaviour has been there since Mono 0.26 (the first 
+mono version i tried) 
+ 
+ 
+Additional Information: 
+This does not happen with MS .NET