5 Replies Latest reply on Jun 29, 2015 2:35 PM by Steve Boyko

    Memory Leak using Event Pipes

    Steve Boyko

      I have a PI-SDK application I wrote in C# that is leaking memory. I've traced it down to the PI portion and I can't understand why it is leaking. I've simplified it to this loop of code:

       

      public static void PIDataEventHandler()

      {

         try

         {

            for (int i = 0; i < lstPoints.Count; i++)

            {

               if (lstPoints[i].datapipe.Count > 0)

               {

                  Array events = lstPoints[i].datapipe.TakeAll();

               }

            }

         }

         catch (Exception e)

         {

            ReportMessage("Error " + e.Message + " handling PI data event(s)" , ReportingLevels.Warning);

         }

      }

       

      lstPoints is an array containing tag definitions and the reference to the event pipe already set up. PIDataEventHandler is the routine that receives new events from PI-SDK. In this sample code I am just throwing the events away but normally I process them... I just wanted to get the simplest code that still has the memory leak.

       

      Does anyone have any suggestions?

        • Re: Memory Leak using Event Pipes
          Steve Boyko

          This is the signup code:

                          datapipe = (IEventPipe2) pipoint.Data.EventPipe;

                          eventpipe = (_DEventPipeEvents_Event)pipoint.Data.EventPipe;

                          eventpipe.OnNewValue += new _DEventPipeEvents_OnNewValueEventHandler(PIEventsToSQL.PIDataEventHandler);

           

          I'm using Visual Studio 2010 and PI-SDK 1.3.8.388.

          • Re: Memory Leak using Event Pipes
            dng

            Hi Steve,

             

            Have you seen this work item 103777? It looks like the problem can exhibit itself when polling frequency and event counts are high. What are the typical event counts?

            Any chance that you can switch over the PI AF SDK instead? See Deprecating the PI SDK

            1 of 1 people found this helpful
              • Re: Memory Leak using Event Pipes
                Steve Boyko

                Typically we receive about 1-2 events/10 seconds for 106 tags, so not a huge number.

                 

                Thanks for the reference to the work item. I tried those settings with no success. I also tried using repeated Take() instead of TakeAll() with no success either.

                 

                I may have to switch to AF SDK but I am surprised that this issue has persisted in the PI-SDK for so long.

                  • Re: Memory Leak using Event Pipes
                    Eugene Lee

                    You can run GC.Collect in the application on a separate thread to remove the memory leak. You can run it on another thread periodically by doing something like

                     

                    void Collect() 
                    { 
                      while(true) 
                      { 
                      GC.Collect(); 
                      Sleep(300000); 
                      } 
                    } 
                    

                     

                     

                    This will do a collection every 5 minutes.

                      • Re: Memory Leak using Event Pipes
                        Steve Boyko

                        Thanks Eugene, I did some debugging and the program is not leaking managed memory (that a garbage collect would clean up) but rather unmanaged memory. A GC.Collect() does nothing for unmanaged memory and in fact if you watch the program's memory consumption you can see the periodic garbage collection that .NET already does. In general I understand it is a bad idea to call GC.Collect() as .NET does a pretty good job of it already.