Question

In the production windows service I had a memory leak in an application.

Service OnStart event: This how my service kick off. I suspect it is because of the events. All this service does is, it subscribes couple of events from third party library and execute those events when the events occurred.

In this code Aggregator is defined as class property

public EventAggregator Aggregator{ get; set;}

Task.Factory
.StartNew(ConnectionManager.AttemptXXXXXXConnection)
                .ContinueWith(task => Aggregator.StartAggregatePublishing(), TaskContinuationOptions.OnlyOnRanToCompletion)
                .ContinueWith(task =>
                                  {
                                      if (task.Exception == null) return;
                                      Logger.Log("Failed to start Events aggregation due to:");

                                      foreach (var exception in task.Exception.InnerExceptions)
                                          Logger.Log(exception.Message);

                                  }, TaskContinuationOptions.OnlyOnFaulted );

And the StartAggregatePublishing method is defined as follows

public IEventHandler<ISystemEvent3> SystemEventHandler
{
   get; set;
}

public IEventHandler<DeviceEvents2EventArgs> DeviceEventHandler
{
     get; set;
}

Here systemEventsManagement and deviceEventManagement belong to third party library events so we can provide our own implementation.

public void StartAggregatePublishing()
{
        Logger.Log("Start event publishing");
        XXXData.Events.SystemEvent += SystemEventHandler.HandleEvent;
        XXXdata.Events.DeviceEvent += DeviceEventHandler.HandleEvent;

        systemEventsManagement.Events       += systemEventsManagement_Events;
        deviceEventsManagement.Event        += deviceEventsManagement_Event;
        processor.Start();
        Logger.Log("System and Device events aggregation started.");
}

Now the game start here with EventAggregator

public EventAggregator( int sleepMills = 1000)
{
        processor = new ThreadPausableProcessor( CheckDeviceStates, HandleException, sleepMills );
}

and the method which runs for every minute is like this

private void CheckDeviceStates()
{
    if ( Device.IsConnected )
    {
            foreach (var deviceState in Skidata.Events.GetAllDeviceStates())
            {
                Logger.Log(string.Format("Checking Handlers for Device id = {0} with Overall state = {1}", deviceState.DeviceId, deviceState.OverallState));

 // Here I am handling pertucular event and logging and reporting stuff.. Nothing major
                DeviceStateHandler.HandleEvent(deviceState);
            }
  // Once I handled all the events I am unhooking all the events and disposing everything.
            Dispose(true);

            int mills;
            ConfigurationManager.RefreshSection("appSettings");
            if ( Int32.TryParse( ConfigurationManager.AppSettings["PollFrequencyInMills"], out mills ) )
            {
                if ( processor.SleepMills != mills )
                {
                    processor.SleepMills = mills;
                    Logger.Log(string.Format("New polling frequency for DeviceStates processor thread = {0} mills", mills));
                }
            }
    }
    else
    {
        Logger.Log("CheckDeviceStates => Not connected to facility.");
    }
}

What I am missing here I don't know I hope I gave enough details.

This is code was written by previous programmer in my place now this code is in production. Can anyone has any clue where exactly it could potentially go wrong.

I have used DotTrace Memory profiler and had couple of snapshots but I couldn't find the problem everything looks ok to me. I can supply those snapshots if needed.

when service re started Private bytes: 31,383,552

after three days of running Private Bytes: 65,584,648

No correct solution

OTHER TIPS

Have you tried Redgate ANTS Memory Profiler ?
It can visualize the relationship between objects and show most memory-hungry objects

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top