I'm trying to retrieve all the contact entity records in a Dynamics CRM 2016 install via a loop. I've got a page size of 1,000 but for some reason Microsoft.Xrm.Sdk.EntityReference and Xrm.Contact keep growing in reference count and memory usage after each loop iteration (e.g., Xrm.Contact's reference count grows by 1,000). Shouldn't each object be disposed of after the foreach loop is done? Each iteration of the while loop adds about 14.6 megabytes to memory usage, eventually causing the program to crash with an out of memory error (there's about 160,000 records so 160 iterations). The code:
private static void GetAllContactRecords()
{
var xrmSC = new Xrm.XrmServiceContext("Xrm");
int pageSize = 1000;
int pageNumber = 1;
var queryEntity = new QueryExpression()
{
ColumnSet = new ColumnSet(true),
EntityName = "contact",
PageInfo = new PagingInfo()
{
PageNumber = pageNumber,
Count = pageSize,
PagingCookie = null
}
};
while (true)
{
EntityCollection results = xrmSC.RetrieveMultiple(queryEntity);
if (results.Entities != null)
{
foreach (Xrm.Contact contact in results.Entities)
{
//do something but commented out for now
}
}
if (results.MoreRecords)
{
queryEntity.PageInfo.PageNumber++;
queryEntity.PageInfo.PagingCookie = results.PagingCookie;
}
else
{
break;
}
}
}
*This post is locked for comments
I have the same question (0)