2

I have an app which is uploading contents of a zip file into a Document Library.

While analyzing memory performance I have noticed the following:

Let's say that somewhere in our code we want to retrieve list of all List that are present on our site. Here is the code:

var lists = _context.Web.Lists;
_context.Load(lists, n => n.Include(x => x.Title));
_context.ExecuteQuery();

In my app I have to do quite a bunch of calls (e.g. retrieving all already existing files that are already uploaded in the document library, etc...).

While analyzing memory usage I have noticed that these object references are not cleared from memory of the ClientContext. This means that I can anytime afterwards in my application simply call the following line:

var AnotherListOfLists = _context.Web.Lists;

and I no longer need to make a call towards _context.ExecuteQuery() as all Web.Lists have already been pre-loaded earlier. Thorough the lifetime of my application I am only using single instance of ClientContext which is bulking up on the references the app is constantly making.

My question would be:

  • Should I regularly dispose my ClientContext and re-create while running long processes (such as uploading more than 50,000 files)
  • Or there is a way how to 'free up' memory in my ClientContext

I am quite new in SharePoint development, therefore any help or suggestions would be more than appreciated.

1

I have the same problem crawling content in Azure jobs. They run out of memory. ClientContext grows as more more is requested in the object tree until it runs out of memory. Some techniques i used:

  • Use REST instead of CSOM where ever possible, so you can keep only what you need
  • Large lists: as you retrieve data from very large lists, process the chunks as they are queried rather than querying all of it into memory and then processing all of it. You could do this by passing an Action into a method that fetches all list items, then invoke it against each batch as they're retrieved before dropping the batch and moving to the next (in my case i set it the batch to null and explicitly called GC.Collect()). This works because ClientContext doesn't retain references to the items.
  • Use multiple ClientContexts. The first might just be to retrieve webs and list title's; then for each list you might be querying fields, role definition bindings and base permissions, so you could do this with a new ClientContext for each list.
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.