Raven.Database.Indexing.BaseBatchSizeAutoTuner.ReduceBatchSizeIfCloseToMemoryCeiling C# (CSharp) Method

ReduceBatchSizeIfCloseToMemoryCeiling() private method

private ReduceBatchSizeIfCloseToMemoryCeiling ( ) : bool
return bool
		private bool ReduceBatchSizeIfCloseToMemoryCeiling()
		{
			if (MemoryStatistics.AvailableMemory >= context.Configuration.AvailableMemoryForRaisingIndexBatchSizeLimit)
			{
				// there is enough memory available for the next indexing run
				return false;
			}

			// we are using too much memory, let us use a less next time...
			// maybe it is us? we generate a lot of garbage when doing indexing, so we ask the GC if it would kindly try to
			// do something about it.
			// Note that this order for this to happen we need:
			// * We had two full run when we were doing nothing but indexing at full throttle
			// * The system is over the configured limit, and there is a strong likelihood that this is us causing this
			// * By forcing a GC, we ensure that we use less memory, and it is not frequent enough to cause perf problems

			GC.Collect(1, GCCollectionMode.Optimized);

			// let us check again after the GC call, do we still need to reduce the batch size?

			if (MemoryStatistics.AvailableMemory > context.Configuration.AvailableMemoryForRaisingIndexBatchSizeLimit)
			{
				// we don't want to try increasing things, we just hit the ceiling, maybe on the next try
				return true;
			}

			// we are still too high, let us reduce the size and see what is going on.

			NumberOfItemsToIndexInSingleBatch = Math.Max(InitialNumberOfItems,
														 NumberOfItemsToIndexInSingleBatch / 2);

			return true;
		}