socket server memory is keep on increasing

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • zoho
    New Member
    • Mar 2012
    • 11

    socket server memory is keep on increasing

    Hi,

    We have socket server which is developed in c# .net 3.5.
    I see server memory keep on increasing whenver client disconnectes and connects.The server disconnects client if client didn;t send valid credentials.
    When client is trying to connect with invalid credentials the memory is keep on increasing.
    Here is the code that handles disconnection.
    Code:
    try
                            {
                                
                                if (state.workSocket != null)
                                {
                                    log.DebugFormat("ssl socket displose,{0},{1}", Doomed, IP);
                                    state.workSocket.Shutdown(SocketShutdown.Both);
                                    state.workSocket.Close(1);
                                    state.workSocket = null;
                                    log.DebugFormat("ssl socket displose complete,{0},{1}", Doomed, IP);
                                }
                            }
                            catch(Exception e1)
                            {
                                log.DebugFormat("error in ssl socket displose,{0},{1},{2}", Doomed, IP,e1);
                            }
    
                            try{
                                if (state.workSslStream != null)
                                {
                                    log.DebugFormat("ssl stream displose,{0},{1}", Doomed, IP);
                                    state.workSslStream.Dispose();
                                    state.workSslStream = null;
                                }
    
                            }
                            catch (Exception e1)
                            {
                                log.DebugFormat("error in ssl stream displose,{0},{1},{2}", Doomed, IP,e1);
                            }
                            
                           state = null;
    
                        }
    Any ideas?
  • rekedtechie
    New Member
    • Feb 2012
    • 51

    #2
    Code:
    try
    {
    //connect
    }
    
    catch
    {
    //exception
    }
    
    finally
    {
    //disconnect
    }

    Comment

    • RhysW
      New Member
      • Mar 2012
      • 70

      #3
      the above code would mean everytime someone connects they disconnect immediately afterwards, don't put it in a block like that

      rekedtechie, things in the finally block are ALWAYS executed, regardless of what happens in the try or catch, with this example the user would connect, may or may not throw an exception, then would be forcibly disconnected the moment it touched on the finally block.

      On topic, perhaps the sockets are not being properly closed, thus each time a new one is opened it increases the memory to allow for it to connect, that's the only thing i can think of that might be happening, if you step through your code in debug mode you might be able to isolate the line or lines of code that are increasing the memory

      Comment

      • zoho
        New Member
        • Mar 2012
        • 11

        #4
        The code I mentioned is only for Disconnect.The disconnect function is being called in read and write operations whenever there is IO xceptionsor clients are being diconnected.

        After disconnect I ran netstat command , I don;t see any connections.

        Thanks

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          Is the SSLStream a wrapper around the socket stream? Should you be disposing of that first?

          You can try inserting this code(yes doubled)
          GC.Collect();GC .Collect();

          It is not a solution, but it can be helpful in finding problems. It will force the garbage collect to free up memory. If calling that code drops your memory down, you probably have half-references to objects somewhere.
          I don't have a better way to explain that, but looking up memory management and best practices (and how the GC works) will be helpful.

          Comment

          • zoho
            New Member
            • Mar 2012
            • 11

            #6
            Yep, I am disposing sockets first and then ssl stream.I tried using tcpClient.getSt ream too instead of socket stream , still no luck.
            I will try adding GC.Collect();tw ice and see what happens.

            Thanks

            Comment

            • zoho
              New Member
              • Mar 2012
              • 11

              #7
              tried adding gc.collect() twice , but didn't work

              thanks
              zoho

              Comment

              • rekedtechie
                New Member
                • Feb 2012
                • 51

                #8
                what if..
                Code:
                try
                {
                //connect
                }
                catch
                {
                //exception
                }
                finally
                {
                GC.Collect();
                }

                Comment

                Working...