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

Comments

mstef created an issue. See original summary.

mstef’s picture

Patch attached.

Before:

$ time drush -v ssg
 [info] Executing: /usr/local/bin/drush batch-process 44 --uri=cms.docksal --root=/var/www/docroot
>  [notice] 494 out of 54301 total items have been processed. memory: 73.368309020996MB, non-peak mem: 51.502227783203MB
>  [notice] 1167 out of 54301 total items have been processed. memory: 73.368309020996MB, non-peak mem: 69.787445068359MB
>  [notice] 1861 out of 54301 total items have been processed. memory: 89.958908081055MB, non-peak mem: 89.241653442383MB
>  [notice] 2610 out of 54301 total items have been processed. memory: 110.16900634766MB, non-peak mem: 109.77597045898MB
>  [notice] 3319 out of 54301 total items have been processed. memory: 130.44128417969MB, non-peak mem: 129.67324829102MB
>  [notice] 4037 out of 54301 total items have been processed. memory: 149.32836151123MB, non-peak mem: 148.93532562256MB
>  [notice] 4708 out of 54301 total items have been processed. memory: 169.14049530029MB, non-peak mem: 168.43495941162MB
>  [notice] 5360 out of 54301 total items have been processed. memory: 187.64639282227MB, non-peak mem: 186.65960693359MB
>  [notice] 6015 out of 54301 total items have been processed. memory: 204.84170532227MB, non-peak mem: 204.07569885254MB
>  [notice] 6650 out of 54301 total items have been processed. memory: 222.57402038574MB, non-peak mem: 221.80598449707MB
>  [notice] 7339 out of 54301 total items have been processed. memory: 242.22951507568MB, non-peak mem: 241.11382293701MB
>  [notice] 7954 out of 54301 total items have been processed. memory: 259.58597564697MB, non-peak mem: 258.2476272583MB
>  [notice] 8590 out of 54301 total items have been processed. memory: 276.87873840332MB, non-peak mem: 276.05210876465MB
>  [notice] 9272 out of 54301 total items have been processed. memory: 296.82150268555MB, non-peak mem: 295.65893554688MB
>  [notice] 9493 out of 54301 total items have been processed. memory: 304.10886383057MB, non-peak mem: 302.59082794189MB
>  [notice] 9663 out of 54301 total items have been processed. memory: 307.49353027344MB, non-peak mem: 306.47158813477MB
>  [notice] 9856 out of 54301 total items have been processed. memory: 313.2205581665MB, non-peak mem: 311.88220977783MB

After:

$ time drush -v ssg
 [info] Executing: /usr/local/bin/drush batch-process 43 --uri=spancms.docksal --root=/var/www/docroot
>  [notice] 130 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 41.674751281738MB
>  [notice] 302 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 46.45263671875MB
>  [notice] 456 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 50.638900756836MB
>  [notice] 647 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 55.128364562988MB
>  [notice] 829 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 60.146385192871MB
>  [notice] 1013 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 64.267562866211MB
>  [notice] 1213 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 45.783508300781MB
>  [notice] 1385 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 50.468147277832MB
>  [notice] 1545 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 53.931190490723MB
>  [notice] 1693 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 57.965065002441MB
>  [notice] 1858 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 62.459419250488MB
>  [notice] 2024 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 46.022277832031MB
>  [notice] 2186 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 50.594245910645MB
>  [notice] 2365 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 55.469276428223MB
>  [notice] 2550 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 59.612495422363MB
>  [notice] 2722 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 47.87060546875MB
>  [notice] 2897 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 52.636817932129MB
>  [notice] 3049 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 47.642547607422MB
>  [notice] 3235 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 49.102294921875MB
>  [notice] 3419 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 49.702819824219MB
>  [notice] 3625 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 50.046600341797MB
>  [notice] 3821 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 50.500915527344MB
>  [notice] 3986 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 50.339874267578MB
>  [notice] 4154 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 54.33277130127MB
>  [notice] 4340 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 51.49658203125MB
>  [notice] 4519 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 51.05443572998MB
>  [notice] 4695 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 51.601989746094MB
>  [notice] 4876 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 52.358062744141MB
>  [notice] 5048 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 51.844421386719MB
>  [notice] 5206 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 52.067932128906MB
>  [notice] 5384 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 53.169250488281MB
>  [notice] 5538 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 56.463203430176MB
>  [notice] 5704 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 53.105163574219MB
>  [notice] 5877 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 53.880828857422MB
>  [notice] 6053 out of 54301 total items have been processed. memory: 73.880165100098MB, non-peak mem: 53.665618896484MB

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.

mstef’s picture

Status: Active » Needs review
gbyte’s picture

Category: Bug report » Task
Status: Needs review » Needs work

Thank 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:

  • Load each entity without caching it (better)
  • Delete the cache of each entity or a bunch of entities after they have been processed (your suggestion)

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?

nterbogt’s picture

Status: Needs work » Needs review
StatusFileSize
new745 bytes

Here 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:

[notice] 779 out of 2619 total items have been processed. memory: 88.243148803711MB, non-peak mem: 86.623573303223MB
[notice] 1577 out of 2619 total items have been processed. memory: 139.42866516113MB, non-peak mem: 132.29336547852MB
[notice] 2327 out of 2619 total items have been processed. memory: 180.83821868896MB, non-peak mem: 176.23033905029MB

After:

[notice] 830 out of 2619 total items have been processed. memory: 57.95288848877MB, non-peak mem: 54.626937866211MB
[notice] 1633 out of 2619 total items have been processed. memory: 62.042144775391MB, non-peak mem: 57.929138183594MB
[notice] 2591 out of 2619 total items have been processed. memory: 64.890434265137MB, non-peak mem: 59.782554626465MB
[notice] 2619 out of 2619 total items have been processed. memory: 64.890434265137MB, non-peak mem: 52.048492431641MB

  • gbyte committed a923efd on 8.x-3.x authored by nterbogt
    Issue #3170261 by mstef, nterbogt, gbyte: Entity static cache can cause...
gbyte’s picture

Status: Needs review » Fixed

@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!

Status: Fixed » Closed (fixed)

Automatically closed - issue fixed for 2 weeks with no activity.