swoole / swoole-src

🚀 Coroutine-based concurrency library for PHP
https://www.swoole.com
Apache License 2.0
18.43k stars 3.16k forks source link

内存泄漏问题 #5513

Closed huelsgp27 closed 51 minutes ago

huelsgp27 commented 4 hours ago

Please answer these questions before submitting your issue.

  1. What did you do? If possible, provide a simple script for reproducing the error.

构造一个大数组(局部变量),并发调用,请求结束后内存没有释放。

docker run -it phpswoole/swoole:4.8 bash

server.php代码如下:

<?php

declare(strict_types=1);

use Swoole\Http\Request;
use Swoole\Http\Response;
use Swoole\Http\Server;

$http = new Server("0.0.0.0", 9501);

$http->on(
    "start",
    function (Server $http) {
        echo "Swoole HTTP server is started.\n";
    }
);
$http->on(
    "request",
    function (Request $request, Response $response) {
         for ($i = 0; $i < 250000; $i++) {
           $arr[] = ["hello, $i"];
        }
        $response->end("Hello, World!\n");
    }
);

$http->start();

并发10请求50次:

seq 1 50 | xargs -I{} -P10 curl 'http://127.0.0.1:9501'
  1. What did you expect to see?

  2. What did you see instead? 使用docker stats查看内存,启动服务后初始内存占用70MB,请求完后250MB。

  3. What version of Swoole are you using (show your php --ri swoole)? swoole

Swoole => enabled Author => Swoole Team team@swoole.com Version => 4.8.13 Built => Oct 12 2024 06:28:58 coroutine => enabled with boost asm context epoll => enabled eventfd => enabled signalfd => enabled cpu_affinity => enabled spinlock => enabled rwlock => enabled sockets => enabled openssl => OpenSSL 1.1.1w 11 Sep 2023 dtls => enabled http2 => enabled json => enabled curl-native => enabled zlib => 1.2.11 mutex_timedlock => enabled pthread_barrier => enabled futex => enabled mysqlnd => enabled async_redis => enabled

Directive => Local Value => Master Value swoole.enable_coroutine => On => On swoole.enable_library => On => On swoole.enable_preemptive_scheduler => Off => Off swoole.display_errors => On => On swoole.use_shortname => On => On swoole.unixsock_buffer_size => 8388608 => 8388608

  1. What is your machine environment used (show your uname -a & php -v & gcc -v) ? Linux f9b85a485f0d 5.15.0-71-generic #78-Ubuntu SMP Tue Apr 18 09:00:29 UTC 2023 x86_64 GNU/Linux

PHP 8.0.28 (cli) (built: May 23 2023 10:31:53) ( NTS ) Copyright (c) The PHP Group Zend Engine v4.0.28, Copyright (c) Zend Technologies

gcc -v Using built-in specs. COLLECT_GCC=gcc COLLECT_LTO_WRAPPER=/usr/lib/gcc/x86_64-linux-gnu/10/lto-wrapper OFFLOAD_TARGET_NAMES=nvptx-none:amdgcn-amdhsa:hsa OFFLOAD_TARGET_DEFAULT=1 Target: x86_64-linux-gnu Configured with: ../src/configure -v --with-pkgversion='Debian 10.2.1-6' --with-bugurl=file:///usr/share/doc/gcc-10/README.Bugs --enable-languages=c,ada,c++,go,brig,d,fortran,objc,obj-c++,m2 --prefix=/usr --with-gcc-major-version-only --program-suffix=-10 --program-prefix=x86_64-linux-gnu- --enable-shared --enable-linker-build-id --libexecdir=/usr/lib --without-included-gettext --enable-threads=posix --libdir=/usr/lib --enable-nls --enable-bootstrap --enable-clocale=gnu --enable-libstdcxx-debug --enable-libstdcxx-time=yes --with-default-libstdcxx-abi=new --enable-gnu-unique-object --disable-vtable-verify --enable-plugin --enable-default-pie --with-system-zlib --enable-libphobos-checking=release --with-target-system-zlib=auto --enable-objc-gc=auto --enable-multiarch --disable-werror --with-arch-32=i686 --with-abi=m64 --with-multilib-list=m32,m64,mx32 --enable-multilib --with-tune=generic --enable-offload-targets=nvptx-none=/build/gcc-10-Km9U7s/gcc-10-10.2.1/debian/tmp-nvptx/usr,amdgcn-amdhsa=/build/gcc-10-Km9U7s/gcc-10-10.2.1/debian/tmp-gcn/usr,hsa --without-cuda-driver --enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu --target=x86_64-linux-gnu --with-build-config=bootstrap-lto-lean --enable-link-mutex Thread model: posix Supported LTO compression algorithms: zlib zstd gcc version 10.2.1 20210110 (Debian 10.2.1-6)

huelsgp27 commented 3 hours ago

5.1和6.0也可以稳定复现,调大循环次数,内存占用也会上涨。

matyhtf commented 51 minutes ago

这不是内存泄漏,你可以多次请求这个接口,可观察到内存占用始终是维持在一个数量,并不会随着请求次数的增加而持续上涨。可使用memory_usage() 测量ZendVM内存占用。

但进程不会归还已释放的内存,而是放置到内存池中复用,这是 Zend MM (内存管理器)的机制,空间换时间。可以使用 USE_ZEND_ALLOC=0 php server.php 关闭Zend MM,或者使用jemalloctcmalloc