Skip to content

Memory leak PHP FPM 8.1 with class entry instances #8646

Closed

Description

@davtur19

Description

Description

A few months ago I tried to migrate to php8.1 but it had a memory leak, so I stayed at 8.0, the memory leak is still present today, while on version 8.0 no problem, the memory remains stable.

Info

Armbian (Ubuntu 20.04.4 LTS)
Linux roc-rk3399-pc 5.15.25-rockchip64
Docker version 20.10.16, build aa7e414

Docker image not working: php8.1-fpm
Docker image working fine: php8.0-fpm

My dockerfile:

FROM php:8.1-fpm

RUN pecl install igbinary
RUN pecl bundle redis && cd redis && phpize && ./configure --enable-redis-igbinary && make && make install
RUN docker-php-ext-enable opcache igbinary redis

RUN mv "$PHP_INI_DIR/php.ini-production" "$PHP_INI_DIR/php.ini"

HEALTHCHECK CMD curl -s 172.17.0.1:8080/ping | grep pong

My php.ini

disable_functions = pcntl_alarm,pcntl_fork,pcntl_waitpid,pcntl_wait,pcntl_wifexited,pcntl_wifstopped,pcntl_wifsignaled,pcntl_wifcontinued,pcntl_wexitstatus,pcntl_wtermsig,pcntl_wstopsig,pcntl_signal,pcntl_signal_get_handler,pcntl_signal_dispatch,pcntl_get_last_error,pcntl_strerror,pcntl_sigprocmask,pcntl_sigwaitinfo,pcntl_sigtimedwait,pcntl_exec,pcntl_getpriority,pcntl_setpriority,pcntl_async_signals,pcntl_unshare,exec,passthru,shell_exec,system,popen,curl_multi_exec,parse_ini_file,show_source,
expose_php = Off
max_execution_time = 60
max_input_time = 60
error_reporting = E_ALL
display_errors = Off
post_max_size = 51M
file_uploads = On
upload_max_filesize = 50M
allow_url_fopen = On
allow_url_include = Off
default_socket_timeout = 60
open_basedir = "/code/:/tmp/"
cgi.force_redirect = 1

[opcache]
; Determines if Zend OPCache is enabled
opcache.enable=1
; The OPcache shared memory storage size.
opcache.memory_consumption=128
; The amount of memory for interned strings in Mbytes.
opcache.interned_strings_buffer=8
; The maximum number of keys (scripts) in the OPcache hash table.
; Only numbers between 200 and 1000000 are allowed.
opcache.max_accelerated_files=10000
; The maximum percentage of "wasted" memory until a restart is scheduled.
opcache.max_wasted_percentage=5
; When disabled, you must reset the OPcache manually or restart the
; webserver for changes to the filesystem to take effect.
opcache.validate_timestamps=1
; Enables or disables file search in include_path optimization
opcache.revalidate_path=1
; If disabled, all PHPDoc comments are dropped from the code to reduce the
; size of the optimized code.
opcache.save_comments=0
; Allow file existence override (file_exists, etc.) performance feature.
opcache.enable_file_override=1
; Check the cache checksum each N requests.
; The default value of "0" means that the checks are disabled.
opcache.consistency_checks=100

#opcache.enable_cli=1
#opcache.jit_buffer_size=100MB
#opcache.jit=1235

php8.1-fpm memory usage (purple line):
image
It had gone up to 800MB in a relatively short time without stopping

php8.0-fpm memory usage (purple line):
image

I don't know what other information to give you because I don't know how to use profiling tools, and I can't test disabling extensions.

PHP Version

8.1

Operating System

Armbian (Ubuntu 20.04.4 LTS)

Activity

  1. iluuu1994 commented on May 27, 2022

    @iluuu1994
    Member

    Well, most likely there's some code in your application that makes PHP leak memory. Is it possible that you isolate the code causing the problem and then provide a snippet that reproduces the leak?

  2. davtur19 commented on May 27, 2022

    @davtur19
    Author

    All the code runs on the webserver and doesn't run for more than 60 seconds, could it still be due to my code?

    It's a little tricky trying to isolate everything, isn't there a way to do it without going crazy?
    Do you have any ideas or guides to link to me?
    Could functions like memory_get_usage() help me?

    A Telegram bot runs on the webserver, so it's mostly curl requests and some redis.

  3. iluuu1994 commented on May 27, 2022

    @iluuu1994
    Member

    @davtur19 Normally any memory that PHP allocates per request gets automatically deallocated after the request ends. It's possible that some code in PHP incorrectly allocates memory (persistently instead of request-bound) which does not automatically get freed and thus the memory keeps creeping up.

    Assuming you can't provide the source code, it's probably easiest to add a die; statement in different places of your application, run a couple hundred times and check if the memory keeps increasing. If not, move the die; back and check again. There are other ways to check if PHP leaks (valgrind or ASAN) but that's not quite as easy to set up.

  4. staabm commented on May 27, 2022

    @staabm
    Contributor

    You might use https://github.com/BitOne/php-meminfo to analyze memory leaks

  5. github-actions commented on Jun 11, 2022

    @github-actions

    No feedback was provided. The issue is being suspended because we assume that you are no longer experiencing the problem. If this is not the case and you are able to provide the information that was requested earlier, please do so. Thank you.

  6. 103 remaining items

  7. sneakyimp commented on Nov 15, 2023

    @sneakyimp

    I think I'm experiencing this memory leak with the very latest PHP 8.2.12 on our production server. We were running PHP 7 in PHP-FPM configuration and memory consumption was stable for years. We migrated our app to another server (with only tiny changes to avoid deprecation warnings about dynamic class properties) and we now see the per-process RAM consumption for our pool of PHP-FPM processes creep up over the course of several days until our server is completely out of memory. Restarting the PHP-FPM pool appears to get all the memory back. Our web app is pretty elaborate so I doubt I can replicate it for anyone, but I do have a bunch of data, including an OOM/Killed dump from the syslog, and some lists of processes were you can see the bloated RAM consumption after about a week versus the lean RAM consumption immediately following a PHP-FPM pool restart.

    I was pretty alarmed to see that PHP 8.1 is going to be closed for bug fixes and this memory leak may not be resolved?

  8. ndossche commented on Nov 15, 2023

    @ndossche
    Member

    I'm going to clarify something: the original reported issue, and some related ones have been long fixed.
    In fact, the original issue wasn't even fpm-specific!

    But this issue report has become a general "report your memleak here" issue, which is kinda ok but confusing for people.

    Without a reproducer or dump it is not clear if the leak is even in fpm, or in an extension. It could even be a leak in a third party extension.

    @sneakyimp whatever issue you're having is different from OP's. A dump is better than nothing but since it comes from a production system it has no debug symbols so it will be hard to figure out what exactly happened.

    And indeed, 8.1 is closed for bugfixes.

    If anyone wants to share a dump, please do so privately via mail. Reminder: never share the dump in public here because a dump usually contains sensitive data like passwords/credentials/private keys.
    Please also include the output of phpinfo() when sending a dump.

  9. sneakyimp commented on Nov 16, 2023

    @sneakyimp

    @nielsdos Thanks for your response.

    Without a reproducer or dump it is not clear if the leak is even in fpm, or in an extension. It could even be a leak in a third party extension.

    @sneakyimp whatever issue you're having is different from OP's. A dump is better than nothing but since it comes from a production system it has no debug symbols so it will be hard to figure out what exactly happened.

    I'm happy to provide any detail I have. Thanks for the warning about the dump possibly containing sensitive info. I was aware of this. That said, I don't think the dump i'm talking about is the same thing as the dump you are referring to. I'm no expert but the information doesn't look very sensitive. I just copied it from the syslog. It goes something like this:

    Nov 14 16:52:44 redacted-hostname kernel: [959137.367888] php invoked oom-killer: gfp_mask=0x1100cca(GFP_HIGHUSER_MOVABLE), order=0, oom_score_adj=0
    Nov 14 16:52:44 redacted-hostname kernel: [959137.367900] CPU: 6 PID: 1496058 Comm: php Not tainted 5.15.0-88-generic #98-Ubuntu
    Nov 14 16:52:44 redacted-hostname kernel: [959137.367903] Hardware name: Xen HVM domU, BIOS 4.7 09/01/2021
    Nov 14 16:52:44 redacted-hostname kernel: [959137.367905] Call Trace:
    Nov 14 16:52:44 redacted-hostname kernel: [959137.367908]  <TASK>
    Nov 14 16:52:44 redacted-hostname kernel: [959137.367911]  show_stack+0x52/0x5c
    ...etc
    
    In the meantime, these two charts should demonstrate first the stable RAM consumption under PHP 7 and after about Nov 5 the increasing RAM consumption that led to the OOM situation.
    ![php-per-process](https://github.com/php/php-src/assets/1297828/68090b3a-7b69-42e5-8c91-6ad81a4d6584)
    ![php-overall-ram](https://github.com/php/php-src/assets/1297828/0041c10e-45db-4a17-a2bf-d577f2b4373c)
    
    

    EDIT: I tried posting the charts here but they are not showing up. I posted them to imgur here.

  10. ndossche commented on Nov 17, 2023

    @ndossche
    Member

    Right, I indeed thought about a different kind of dump. The one you posted is not sensitive indeed.
    From the charts it seems like the memory usages spikes shortly after a memory drop. So could it be that it spikes after an opcache restart?

  11. sneakyimp commented on Nov 20, 2023

    @sneakyimp

    @nielsdos It looks like the adjustment to pm.max_requests is working, and memory consumption once again seems stable. Perhaps the default php-fpm config should have a nonzero value for this setting?

  12. ndossche commented on Nov 20, 2023

    @ndossche
    Member

    Interesting workaround. It probably hides the real issue, or somehow some children aren't properly terminated? I don't know.
    Anyway, cc @bukka perhaps for ideas

  13. wizardist commented on Dec 21, 2023

    @wizardist

    Version 8.2.5 changelog states:

    Fixed bug GH-8646 (Memory leak PHP FPM 8.1).

    Should this be closed then?

  14. ndossche commented on Dec 21, 2023

    @ndossche
    Member

    Version 8.2.5 changelog states:

    Fixed bug GH-8646 (Memory leak PHP FPM 8.1).

    Should this be closed then?

    The original bug reported in this issue was fixed. It was reopened because people use this issue as a generic leak issue thread.
    Perhaps this should be renamed and closed because this thread became a bit messy.
    cc @iluuu1994 wdyt?

  15. iluuu1994 commented on Dec 21, 2023

    @iluuu1994
    Member

    Sure! If the original problem is solved we should close this and create new issues for anything distinctly unrelated.

  16. changed the title [-]Memory leak PHP FPM 8.1[/-] [+]Memory leak PHP FPM 8.1 with class entry instances[/+] on Dec 21, 2023
  17. added a commit that references this issue on Feb 3, 2024
    ff62d11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions