ExecutionContexts problem with async sockets

Use this forum to read, ask or inform about memory issues in the .NET Framework and third party components.

Moderator: SciTech Software

ExecutionContexts problem with async sockets

Postby Jungler » Tue May 06, 2014 6:47 pm

Hi every one.
I`m made simple app, to discover memory leaks, here it is (server code). 90% specialy taken from MSDN
Code: Select all
        public class StateObject
        {
            // Client socket.
            public Socket workSocket = null;
            // Size of receive buffer.
            public const int BufferSize = 256;
            // Receive buffer.
            public byte[] buffer = new byte[BufferSize];
            // Received data string.
            public StringBuilder sb = new StringBuilder();
        }

        static void ReceiveCallback(IAsyncResult ar)
        {
            try
            {
                StateObject state = (StateObject)ar.AsyncState;
                Socket client = state.workSocket;
                // Read data from the remote device.
                int bytesRead = client.EndReceive(ar);

                if (bytesRead > 0)
                {
                    StateObject st = new StateObject();
                    st.workSocket = client;
                    clientSocket.BeginReceive(st.buffer, 0, st.buffer.Length, 0, ReceiveCallback, st);
                }
            }
            catch (Exception error)
            {
                Console.WriteLine(error.ToString());
            }
        }

        static void StartReceive()
        {
            try
            {
                StateObject state = new StateObject();
                state.workSocket = clientSocket;
                clientSocket.BeginReceive(state.buffer, 0, state.buffer.Length, 0, ReceiveCallback, state);
            }
            catch (Exception error)
            {
                Console.WriteLine(error.ToString());
            }
        }



Server create socket, and begin StartReceive. After this client connect to server, and starting send packets.
And result of this you can see on screenshot: (link - http://pumpshooter.com/jR30eFbz)
Image

After this example i`m testing another async functions, like SendAsync, await\async, and delegate.BeginInvoke - all them make many many undisposable ExecutionContexts and provide memory leaks?

How i can fix this? Look like any c# async methods have memory leaks
Jungler
 
Posts: 1
Joined: Tue May 06, 2014 6:39 pm

Re: ExecutionContexts problem with async sockets

Postby Andreas Suurkuusk » Wed May 07, 2014 9:04 am

An undisposed instance indicates that an instance has been collected (or at least finalized) without being properly disposed. Since the instance have been collected, having undisposed instances do not indicate a memory or resource leak. It does however indicate that the memory or resource utilization might not be optimal (e.g. by delaying the release of a resource and causing finalization of instances).

In particular, the ExecutionContext class disposes other instances in its Dispose method, so undisposed ExecutionContext instances can cause other instances to be undisposed as well, but it will not cause a memory leak. Your screenshot shows that you have a high amount of undisposed instances, but very few live instances (i.e. there's no indication of a memory leak).

If possible, you should try to make sure that all instances are correctly disposed. However, it's pretty common that the .NET Framework itself causes undisposed instances and there's nothing you can do about it.
Best regards,

Andreas Suurkuusk
SciTech Software AB
Andreas Suurkuusk
 
Posts: 962
Joined: Wed Mar 02, 2005 7:53 pm
Location: Sweden


Return to Known Memory Issues

Who is online

Users browsing this forum: No registered users and 1 guest

SciTech Software logo

© Copyright 2001-2016. SciTech Software AB
All rights reserved.


SciTech Software AB
Kartvägen 21
SE-175 46 Järfälla
Sweden


E-mail: mail@scitech.se

Telephone: +46-706868081

cron