Mongodb aggregate out of memory
<p>I'm using mongodb aggregate to sample documents from a large collection.</p>

<p><a href="https://docs.mongodb.com/manual/reference/operator/aggregation/sample/" rel="nofollow">https://docs.mongodb.com/manual/reference/operator/aggregation/sample/</a></p>

<p>After making several consecutive calls, I see the memory of mongodb climbing up, and after around the 12th call, it crashes with OutOfMemory error.</p>

<p>How can I tell Mongodb to free up the memory after it has finished processing a query?</p>

<p>It turns out the issue was the storage engine cache. I'm using an EC2 instance, and it resulted in OOM error there. I've been able to solve it by assigning a smaller cache size like this:</p>

<pre><code>mongod --dbpath /a/path/to/db --logpath /a/path/to/log --storageEngine wiredTiger --wiredTigerEngineConfigString="cache_size=200M" --fork
</code></pre>

python mongodb pymongo aggregation-framework