Skip to content

Memory Leak in lib/private/Preview/Generator.php -> generatePreviews #21995

@elijahnyp

Description

@elijahnyp

How to use GitHub

  • Please use the 👍 reaction to show that you are affected by the same issue.
  • Please don't comment if you have no relevant information to add. It's just extra noise for everyone subscribed to this issue.
  • Subscribe to receive notifications on status change and new comments.

Steps to reproduce

  1. Cause generatePreviews to be executed many times from the same script (like running the previewgenerator app) with no existing previews - problem only occurs if a preview hasn't already been generated
  2. Watch memory disappear until memory limit is reached (regardless of what the limit is set at - tried up to an 8gb memory limit)

Expected behaviour

You shouldn't run out of memory

Actual behaviour

You run out of memory

Investigation Already Completed

  • modified previewgenerator generate-all command to dump memory profile with php_memory_profiler
  • determined that imagecreatefromstring being called from OC_Image::loadFromData was causing the memory issues (over 97% of memory used by imagecreatefromstring (see image below)
  • traced call chain in nextcloud to the call loading $maxPreviewImage in the generatePreviews function
  • modified generatePreviews to check if $maxPreviewImage is !== null prior to return, and if it is to call imagedestroy($maxPreviewImage->resource())
                if($maxPreviewImage !== null){
                        imagedestroy($maxPreviewImage->resource());
                }

                return $preview;
  • problem went away - went from an OOM condition after 5-30 images to hundreds of previews being generated and memory consumption staying manageable

Is this the best solution? 🤷 does it work for this problem? It appears so.

It seems there may be a memory leak in the underlying php imagecreatefromstring function but this work-around allows things to function as expected, at least for me. I usually do most of my coding these days in python and go, so I may not be the best person to submit a PR for this.

Server configuration

Operating system:
Official nextcloud docker image

Web server: apache - built into docker image base with all optional packages installed per nextcloud docker documentation

Database: mysql

PHP version: 7.4.8

Nextcloud version: 19.0.1

Updated from an older Nextcloud/ownCloud or fresh install: updated from 19.0.0 where issue was also observed

Where did you install Nextcloud from: dockerhub + addition of extra packages

2020-07-24 20_36_39-C__Users_elijah ARTYNET_scratch_callgrind out  unknown

Metadata

Metadata

Assignees

No one assigned

    Labels

    0. Needs triagePending check for reproducibility or if it fits our roadmapbug

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions