System.OutOfMemoryException is thrown when sending documents to Solr


Description

Rebuilding or refreshing the search index might fail with an exception that can be found in the logs:

Exception: System.OutOfMemoryException
Message: Exception of type 'System.OutOfMemoryException' was thrown.
Source: mscorlib
   at System.Text.StringBuilder.ToString()
   at System.Xml.Linq.XNode.GetXmlString(SaveOptions o)
   at SolrNet.Commands.AddCommand`1.ConvertToXml()
   at SolrNet.Commands.AddCommand`1.Execute(ISolrConnection connection)
   at SolrNet.Impl.LowLevelSolrServer.SendAndParseHeader(ISolrCommand cmd)
   at Sitecore.ContentSearch.SolrProvider.SolrBatchUpdateContext.Commit()
   at Sitecore.ContentSearch.SolrProvider.SolrSearchIndex.PerformRebuild(Boolean resetIndex, Boolean optimizeOnComplete, IndexingOptions indexingOptions, CancellationToken cancellationToken)
   at Sitecore.ContentSearch.SolrProvider.SolrSearchIndex.Rebuild(Boolean resetIndex, Boolean optimizeOnComplete)

Solution

Option 1

To prevent the issue, reduce the value of the ContentSearch.Update.BatchSize setting, by creating a configuration patch file in the \App_Config\Include\zzz folder, for example:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:search="http://www.sitecore.net/xmlconfig/search/">
  <sitecore search:require="solr">
    <settings>
      <setting name="ContentSearch.Update.BatchSize" value="25" />
    </settings>
  </sitecore>
</configuration>

Note: This might result in a slower index update/rebuild rate and increased resource consumption on the Solr server.

Option 2

The error does not necessarily mean that the application has insufficient memory. The issue might be caused by the Large Object Heap (LOH) fragmentation, which prevents allocation of large objects like a batch of documents with a lot of content.

To identify the fields with a lot of content, use the Sitecore Crawling log file:

  1. Enable the DEBUG logging level and verbose logging as described here. (Note that the log files might grow significantly when those are enabled.)
  2. Rebuild the search index and check the logs.

To exclude the fields with a lot of content from being indexed, extend the list of the excluded fields by creating a configuration patch file in the \App_Config\Include\zzz folder:

<?xml version="1.0" encoding="utf-8" ?>
<configuration xmlns:patch="http://www.sitecore.net/xmlconfig/" xmlns:role="http://www.sitecore.net/xmlconfig/role/" xmlns:search="http://www.sitecore.net/xmlconfig/search/">
  <sitecore search:require="solr">
    <contentSearch>
      <indexConfigurations>
        <defaultSolrIndexConfiguration>
          <documentOptions>
            <exclude hint="list:AddExcludedField">
              <FieldName>{ID_of_the_field}</FieldName>
            </exclude>
          </documentOptions>
        </defaultSolrIndexConfiguration>
      </indexConfigurations>
    </contentSearch>
  </sitecore>
</configuration>