AnsweredAssumed Answered

How can I control AFSDK memory usage?

Question asked by SvenBatalla on Jun 26, 2015
Latest reply on Jul 1, 2015 by nagarwal

We are using the AFSDK to retrieve and process data for 20,000 elements.  The .NET server we've created (after much performance-based tweaking) appears to be running fine, but the memory usage is quite large (500MB at peak).  This could be a function of .NET, but memory profilers are pointing at the AFAttribute class as the biggest culprit of memory usage.  So the question is:  can I somehow bring the memory footprint of the AFSDK under control?


Before it's suggested, I've done the unadvised manual garbage collection and finalization (4 successive times to be sure) to no avail.


Here is what we've done (as it applies to this question):


Please excuse any typos or complexity in the code example.  This was written for brevity.


using(PISystem pisystem = new PISystems()["My PIAF Server"])
   // Connect to the server

   // Open the needed database
   AFDatabase db = pisystem.Databases["My PIAF Database"];
   db.Refresh(); // This is done because we cannot have cached info in this service

   // Get the list of needed elements
   AFElementTemplate myTemplate = db.ElementTemplates["My Template"];
   AFAttributeTemplate myAttrTemplate = myTemplate.AttributeTemplates["My Attr Template #1"];
   IList<AFElement> elements = AFElement
         new[]{new AFAttributeValueQuery(myAttrTemplate, AFSearchOperator.Equal, true)},

   // To aid in upcoming element & attribute usage, preload all element data

   // Get snapshot values (in bulk) for all the elements in the list
   var attributeList = new AFAttributeList();
   foreach(AFElement element in elements)
      attributeList.Add(element.Attributes["My Tag Attribute"]);
   AFValues snapshotValues = attributeList.GetValue();

   // At this point, the list of snapshot values and elements are used to perform a number of
   // calculations.  Nothing more that is done is relevant to any additional memory usage, so it is
   // not shown here.


The above code is run via a thread timer (which runs once per minute).  So the result is (or should be) that leaving the shown "using" scope (or even the method scope that contains it) should result in the objects being disposed by the garbage collector (eventually anyways).  I've wrapped all disposable items with a "using" statement, so there is nothing more than I can do.  Is there a call that can make (or should make) to the AFSDK that will clear the memory usage, or is this the optimal way to use the AFSDK and I can trust that .NET and Windows will manage the memory as required on the server?


Many thanks in advance!