Problem/Motivation
Testing on a site with 55K nodes, all of which are indexable. Executing drush ssg to do a full sitemap regeneration. I amended the batch status message to include the peak memory usage (memory_get_peak_usage()). The memory starts at around 40-70mb, and slowly grows, ending at around 1.9gb. It appears to increase by around 5mb per every 200 entities indexed. This led me to believe that the entity static cache was the culprit, because even though the queue worker isn't holding on to all of the entities that are loaded, Drupal is.
Steps to reproduce
Amend BatchTrait::doBatchGenerateSitemap() to add memory usage to the batch context message:
IE:
$mem = (memory_get_peak_usage(false)/1024/1024) . "MB";
$mem_non_peak = (memory_get_usage()/1024/1024) . "MB";
$context['message'] = t('@indexed out of @total total items have been processed. memory: @mem, non-peak mem: @memp', [
'@indexed' => $processed_element_count, '@total' => $original_element_count, '@mem' => $mem, '@memp' =>
$mem_non_peak]);
$context['finished'] = $original_element_count > 0 ? ($processed_element_count / $original_element_count) : 1;
Create 55K+ indexable entities.
Run drush ssg.
Proposed resolution
Inject the service entity.memory_cache in to QueueWorker and execute the following after the conclusion of each loop, or once before the function ends:
\Drupal::service('entity.memory_cache')->deleteAll();
or more accurately
$this->memoryCache->deleteAll();
After including this, the peak memory usage remained at ~70mb for the entire operation, which takes around 45 minutes (locally).
Remaining tasks
Confirm the resolution and write a patch.
User interface changes
None
API changes
None
Data model changes
None
| Comment | File | Size | Author |
|---|---|---|---|
| #5 | simple_sitemap-memory_issues-3170261-5.patch | 745 bytes | nterbogt |
| #2 | 3170261-simple_sitemap-flush_entity_memory_cache-2.patch | 2.4 KB | mstef |
Comments
Comment #2
mstef commentedPatch attached.
Before:
After:
The downside is the speed is much slower, as you can see how many less items are indexed per batch. Perhaps we can better limit which entities are cleared.
Comment #3
mstef commentedComment #4
gbyteThank you for this investigative work! I did some tests and here is what I found:
The additional process of cache clearing does not really affect the generation speed. What does, is the fact that after the first cache clear, all following entities are loaded without cache. What I mean is, your solution does not affect speed more negatively than a single cache clear before generation would.
To keep the speed and still fix the memory usage problem, I see the following two solutions:
This way we could use the cached entities for generation while not caching those again. Unfortunately I couldn't find a way to load an entity without caching it (please correct me if I'm wrong), so I think the second option is the only option. Would you be willing to write a patch that more precisely clears caches?
Comment #5
nterbogt commentedHere is my solution to this problem. The resetCache() method on the entity storage will invalidate the entity in the memory cache. Garbage collection is doing its job and cleaning it up, as far as I can tell.
Wrapped it in a method check because it's possible a custom entity uses a storage class that doesn't have it.
In terms of performance, I'm getting results that are all over the place... anything from 2x faster, to 10% slower. Would like to know what others are getting.
Before:
After:
Comment #7
gbyte@nterbogt I was testing a similar solution in #4. The mixed generation speed results went away as soon as I cleared cache before each test run.
This solution is good enough, thanks guys!