web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Microsoft Dynamics CRM (Archived)

RAM Usage When Retrieving Dynamics CRM Contact Entity Records

(0) ShareShare
ReportReport
Posted on by 40

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)
  • ashlega Profile Picture
    34,477 on at

    You can't say when garbage collection would happen, but you might try to call gc.collect - may or may not help:

    msdn.microsoft.com/.../xe0c2357(v=vs.110).aspx

    I think it might also help if you isolated all those "references" into a separate function (for example, try passing page number to the function and calling it in a loop from somewhere else. If it helps, you might be able to figure out what reference is getting stuck from there)

  • NeilN Profile Picture
    40 on at

    Explicit garbage collection didn't help and neither did isolating the retrieve function in a loop.  I made some quick and dirty changes to support this:

               cookie = GetAllContactRecords(1, "");

               cookie = GetAllContactRecords(2, cookie);

               cookie = GetAllContactRecords(3, cookie);

               cookie = GetAllContactRecords(4, cookie);

    and explicitly disposed of xrmSC in the function.  Still getting 14.6 megabytes added per function call and after the function is called for the fourth time, this is in memory:

    shot_5F00_030.jpg

  • ScottDurow Profile Picture
    21 on at

    Does the above code throw an out of memory exception even with the commented out code?

    15MB really isn't that much for memory usage - and as Alec says, the Garbage collector is non deterministic and so will only run when it needs to.

  • NeilN Profile Picture
    40 on at

    Yes it does. 15 megabytes x 160 iterations = 2.4 gigabytes of RAM.  I don't understand why the objects are not re-initialized after the private static string GetAllContactRecords(pageNumber, cookie) function returns.  What is causing objects like Xrm.Contact to be "appended to" instead?

  • Verified answer
    ashlega Profile Picture
    34,477 on at

    Wondering if it may have something to do with caching. Maybe see if disabling caching would help?

    msdn.microsoft.com/.../gg695805.aspx

    (look for "The following shows how to disable caching:" on that page)

  • NeilN Profile Picture
    40 on at

    That did the trick, thank you.  Bit of an oversight of Microsoft's part not to mention this in their documentation covering entity retrieval.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > 🔒一 Microsoft Dynamics CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans