locked
SignalR memory leak RRS feed

  • Question

  • Hello,

    We are facing memory leak issue in one of our window service application. after doing analysis using memory profiler we found that object of 'Microsoft.AspNet.SignalR.Messaging.Message' class is increasing day to day.

    For e.g. after 2 hour of running service instance count was 1768 instance with 99008 bytes.

    And after 1 and half day days it is increased to 4606 instance with 257536 bytes.

    We are using 2.0.3.0 version of signalr.

    Following is the implementation of my hub class in window service.

     [Microsoft.AspNet.SignalR.Hubs.HubName("messages")]
        public class Messages : Hub
        {
            public void GetMessage(String jsonString)
            {
                try
                {
                    JavaScriptSerializer json_serializer = new JavaScriptSerializer();
                    MessageInterface.RequestMessage req = (MessageInterface.RequestMessage)json_serializer.Deserialize<MessageInterface.RequestMessage>(jsonString);
                    req.ConnectionId = Context.ConnectionId;
                    var ipAddress = GetIpAddress();
                    req.IPAddress = ipAddress;
                    if (string.IsNullOrEmpty(req.Workstation))
                        req.Workstation = req.IPAddress;
                    Logger.Trace("Request from web ", LogLevel.RequestRespnose, req.MsgType.ToString() + ";" + req.Resourcenumber, req);
                    json_serializer = null;
                    Task.Factory.StartNew(() => new Communication().SendRequest(req)).ContinueWith(r =>
                    {
                        if (r.Exception != null)
                            Logger.Log(r.Exception);
                    }).ContinueWith(t => t.Dispose());
                }
                catch (Exception Ex)
                {
                    Logger.Log(Ex);
                }
            }

            protected string GetIpAddress()
            {
                string ipAddress;
                object tempObject;

                Context.Request.Environment.TryGetValue("server.RemoteIpAddress", out tempObject);

                if (tempObject != null)
                {
                    ipAddress = (string)tempObject;
                }
                else
                {
                    ipAddress = "";
                }

                return ipAddress;
            }

      public static void sendMessage(Message message, String ConnectionId = "")
            {
                try
                {
                   
                    Task.Factory.StartNew<Message>(() =>
                    {
                        Logger.Trace((string.IsNullOrEmpty(message.ConnectionId) ? "BroadCast Message" : "Sending to web with connectionid " + message.ConnectionId), LogLevel.RequestRespnose, message.MessageNo, data: message);
                        return message;
                    }).ContinueWith((t) =>
                        {
                            if (t.Result is FlagsData)
                            {
                                var context = GlobalHost.ConnectionManager.GetHubContext<Messages>();
                                context.Clients.Client(t.Result.ConnectionId).recievedflag((FlagsData)message);
                            }
                            else
                            {
                                var context1 = GlobalHost.ConnectionManager.GetHubContext<Messages>();
                                if (!string.IsNullOrEmpty(ConnectionId))
                                {
                                    if (t.Result.MonitorType == MonitorType.LAW && t.Result.MessageNo == "083")
                                        context1.Clients.Group(MonitorType.LAW.ToString().ToUpper()).addMessage(t.Result);
                                    else
                                        context1.Clients.Client(ConnectionId).addMessage(t.Result);
                                }
                                else
                                {
                                    if ((t.Result as Resource).MonitorType == MonitorType.FIRE && t.Result.MessageNo == "083")
                                    {
                                        if (!string.IsNullOrEmpty(t.Result.ConnectionId))
                                            context1.Clients.Client(ConnectionId).addMessage(t.Result);
                                        else
                                            context1.Clients.Group("FIRE" + (t.Result as Resource).Resourcenumber.ToUpper()).addMessage(t.Result);
                                    }
                                    else
                                    {
                                        context1.Clients.Group((t.Result as Resource).MonitorType.ToString().ToUpper()).addMessage(t.Result);
                                        if (t.Result.MessageNo == "022")
                                            context1.Clients.Group(MonitorType.FIRE.ToString().ToUpper()).addMessage(t.Result);
                                    }
                                }
                            }
                        }).ContinueWith(r =>
                        {
                            if (r.Exception != null)
                                Logger.Log(r.Exception);
                            r.Dispose();
                        });
                }
                catch (AggregateException Ex)
                {
                    Logger.Log(Ex.InnerException);
                }
            }

      public void Join(String groupname)
            {
                try
                {
                    if (!string.IsNullOrEmpty(groupname))
                    {
                        if (groupname.ToUpper() == "DESKTOP")
                        {
                            var context = GlobalHost.ConnectionManager.GetHubContext<Messages>();
                            String jsonString = JsonConvert.SerializeObject(Global.Locator.RetrieveWebService("Monitor"));
                            context.Clients.Client(Context.ConnectionId).DeskEvent("URL", jsonString);
                            jsonString = JsonConvert.SerializeObject(Global.Locator.RetrieveService("NCIC"));
                            context.Clients.Client(Context.ConnectionId).DeskEvent("NCICConfig", jsonString);
                            jsonString = "";
                        }
                        else if (groupname.StartsWith("UPDATE"))
                        {
                            String ResNo = groupname.Replace("UPDATE", "");
                            if (OnlineResource.UniqueInstance.GetResource(ResNo) != null)
                            {
                                LoginDetails.UniqueInstance.UpdateDesktopLogin(ResNo, Context.ConnectionId);
                                DesktopClient(ResNo, ResNo, OnlineResource.UniqueInstance.GetResource(ResNo));
                            }
                        }
                        else
                            Groups.Add(Context.ConnectionId, groupname.ToUpper());
                    }
                }
                catch (Exception Ex)
                {
                    Logger.Log(Ex);
                }
            }

    }

    Following is the startup class implementation


    [assembly: OwinStartup(typeof(XMWebInterface.Signalr.XMStartup))]

    namespace XMWebInterface.Signalr
    {
        public class XMStartup
        {
            public void Configuration(IAppBuilder app)
            {
               
                Int32 disconnectTimeout = 10;
                if (!string.IsNullOrEmpty(System.Configuration.ConfigurationSettings.AppSettings["SignalrDisconnect"]))
                {
                    disconnectTimeout = Convert.ToInt32(System.Configuration.ConfigurationSettings.AppSettings["SignalrDisconnect"]);
                }
                GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromMinutes(disconnectTimeout);
                //GlobalHost.Configuration.DisconnectTimeout = TimeSpan.FromSeconds(40);
                GlobalHost.Configuration.KeepAlive = TimeSpan.FromSeconds(5);
               
                var config = new HubConfiguration
                {
                    EnableJSONP = true
                   
                };
                app.MapSignalR(config);
            }
        }
    }

    Please review and assist us if we are doing anything wrong.

    Thanks in advance

    Jignesh






    Friday, December 26, 2014 6:15 AM

All replies

  • Hi Jignesh,

    I am afraid you posted in an inappropriate forum.

    For the  SignalR you can ask here:

    http://forums.asp.net/1254.aspx/1?ASP+NET+SignalR 

    Now i will put your thread to "off-topic", thanks for your understanding.

    By the way, i have found a sample about Self-Hosting SignalR in a Windows Service.Hope it could help you.

    https://code.msdn.microsoft.com/SignalR-self-hosted-in-6ff7e6c3/

    Good luck.

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Monday, December 29, 2014 2:43 AM