首页
下载
文档
社区
视频
捐赠
源代码
赞助商
AOT 编译器
AI 助理
商业产品
PHP AOT 原生编译器
Swoole-Compiler 代码加密器
CRMEB 新零售社交电商系统
登录
注册
全部
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
发表新帖
内存泄露问题
### 问题描述 线上服务worker内存上涨非常快, 千万以上请求量后内存仍然在涨 ### Swoole版本,PHP版本,以及操作系统版本信息 ``` PHP 7.4.12 (cli) (built: Nov 11 2020 13:35:32) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.12, Copyright (c), by Zend Technologies ``` ``` swoole Swoole => enabled Author => Swoole Team <team@swoole.com> Version => 4.5.7 Built => Nov 11 2020 13:37:00 coroutine => enabled epoll => enabled eventfd => enabled signalfd => enabled cpu_affinity => enabled spinlock => enabled rwlock => enabled openssl => OpenSSL 1.1.1h 22 Sep 2020 http2 => enabled zlib => 1.2.7 mutex_timedlock => enabled pthread_barrier => enabled futex => 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 ``` ``` centos 7.3 kernel 4.18.20 OpenSSL 1.1.1h 22 Sep 2020 ``` ### valgrind.log ``` $ USE_ZEND_ALLOC=0 valgrind --leak-check=full --show-leak-kinds=all --error-limit=no --log-file=valgrind.log php script.php ==58394== 24 bytes in 1 blocks are definitely lost in loss record 4,224 of 15,037 ==58394== at 0x4C27BE3: malloc (vg_replace_malloc.c:299) ==58394== by 0x7E5BAF: zend_register_functions (zend_API.c:2297) ==58394== by 0x7E6D65: do_register_internal_class (zend_API.c:2708) ==58394== by 0x807FA1: zend_register_weakref_ce (zend_weakrefs.c:194) ==58394== by 0x7F4BF6: zm_startup_core (zend_builtin_functions.c:313) ==58394== by 0x7E4E44: zend_startup_module_ex (zend_API.c:1860) ==58394== by 0x7E4EDB: zend_startup_module_zval (zend_API.c:1875) ==58394== by 0x7F1802: zend_hash_apply (zend_hash.c:1812) ==58394== by 0x7E5189: zend_startup_modules (zend_API.c:1986) ==58394== by 0x7832E5: php_module_startup (main.c:2332) ==58394== by 0x8640DC: php_cli_startup (php_cli.c:410) ==58394== by 0x4627D3: main (php_cli.c:1326) -- ==58394== 24 bytes in 1 blocks are definitely lost in loss record 4,225 of 15,037 ==58394== at 0x4C27BE3: malloc (vg_replace_malloc.c:299) ==58394== by 0x7B98C8: __zend_malloc (zend_alloc.c:2976) ==58394== by 0x7C691E: zend_compile_params (zend_compile.c:5347) ==58394== by 0x7CF21B: zend_compile_func_decl (zend_compile.c:6050) ==58394== by 0x7C7DE4: zend_compile_expr (zend_compile.c:8716) ==58394== by 0x7CAD89: zend_compile_args (zend_compile.c:3059) ==58394== by 0x7CB197: zend_compile_call_common (zend_compile.c:3136) ==58394== by 0x7CBABD: zend_compile_static_call (zend_compile.c:4032) ==58394== by 0x7CAC30: zend_compile_var (zend_compile.c:8744) ==58394== by 0x7C7D14: zend_compile_expr (zend_compile.c:8613) ==58394== by 0x7CE2BA: zend_compile_stmt (zend_compile.c:8577) ==58394== by 0x7CEFD6: zend_compile_stmt_list (zend_compile.c:5266) -- ==58394== 3,568 (3,168 direct, 400 indirect) bytes in 132 blocks are definitely lost in loss record 14,928 of 15,037 ==58394== at 0x4C27BE3: malloc (vg_replace_malloc.c:299) ==58394== by 0x7B98C8: __zend_malloc (zend_alloc.c:2976) ==58394== by 0x7C691E: zend_compile_params (zend_compile.c:5347) ==58394== by 0x7CF21B: zend_compile_func_decl (zend_compile.c:6050) ==58394== by 0x7CE5DB: zend_compile_stmt (zend_compile.c:8545) ==58394== by 0x7CEFD6: zend_compile_stmt_list (zend_compile.c:5266) ==58394== by 0x7CE43B: zend_compile_stmt (zend_compile.c:8489) ==58394== by 0x7CFD25: zend_compile_class_decl (zend_compile.c:6477) ==58394== by 0x7D14FE: zend_compile_top_stmt (zend_compile.c:8464) ==58394== by 0x7D14D6: zend_compile_top_stmt (zend_compile.c:8453) ==58394== by 0x7A8F52: zend_compile (zend_language_scanner.c:614) ==58394== by 0x7AA807: compile_string (zend_language_scanner.c:779) ==58394== 24 bytes in 1 blocks are definitely lost in loss record 4,224 of 15,037 ==58394== at 0x4C27BE3: malloc (vg_replace_malloc.c:299) ==58394== by 0x7E5BAF: zend_register_functions (zend_API.c:2297) ==58394== by 0x7E6D65: do_register_internal_class (zend_API.c:2708) ==58394== by 0x807FA1: zend_register_weakref_ce (zend_weakrefs.c:194) ==58394== by 0x7F4BF6: zm_startup_core (zend_builtin_functions.c:313) ==58394== by 0x7E4E44: zend_startup_module_ex (zend_API.c:1860) ==58394== by 0x7E4EDB: zend_startup_module_zval (zend_API.c:1875) ==58394== by 0x7F1802: zend_hash_apply (zend_hash.c:1812) ==58394== by 0x7E5189: zend_startup_modules (zend_API.c:1986) ==58394== by 0x7832E5: php_module_startup (main.c:2332) ==58394== by 0x8640DC: php_cli_startup (php_cli.c:410) ==58394== by 0x4627D3: main (php_cli.c:1326) -- ==58394== 24 bytes in 1 blocks are definitely lost in loss record 4,225 of 15,037 ==58394== at 0x4C27BE3: malloc (vg_replace_malloc.c:299) ==58394== by 0x7B98C8: __zend_malloc (zend_alloc.c:2976) ==58394== by 0x7C691E: zend_compile_params (zend_compile.c:5347) ==58394== by 0x7CF21B: zend_compile_func_decl (zend_compile.c:6050) ==58394== by 0x7C7DE4: zend_compile_expr (zend_compile.c:8716) ==58394== by 0x7CAD89: zend_compile_args (zend_compile.c:3059) ==58394== by 0x7CB197: zend_compile_call_common (zend_compile.c:3136) ==58394== by 0x7CBABD: zend_compile_static_call (zend_compile.c:4032) ==58394== by 0x7CAC30: zend_compile_var (zend_compile.c:8744) ==58394== by 0x7C7D14: zend_compile_expr (zend_compile.c:8613) ==58394== by 0x7CE2BA: zend_compile_stmt (zend_compile.c:8577) ==58394== by 0x7CEFD6: zend_compile_stmt_list (zend_compile.c:5266) -- ==58394== 3,568 (3,168 direct, 400 indirect) bytes in 132 blocks are definitely lost in loss record 14,928 of 15,037 ==58394== at 0x4C27BE3: malloc (vg_replace_malloc.c:299) ==58394== by 0x7B98C8: __zend_malloc (zend_alloc.c:2976) ==58394== by 0x7C691E: zend_compile_params (zend_compile.c:5347) ==58394== by 0x7CF21B: zend_compile_func_decl (zend_compile.c:6050) ==58394== by 0x7CE5DB: zend_compile_stmt (zend_compile.c:8545) ==58394== by 0x7CEFD6: zend_compile_stmt_list (zend_compile.c:5266) ==58394== by 0x7CE43B: zend_compile_stmt (zend_compile.c:8489) ==58394== by 0x7CFD25: zend_compile_class_decl (zend_compile.c:6477) ==58394== by 0x7D14FE: zend_compile_top_stmt (zend_compile.c:8464) ==58394== by 0x7D14D6: zend_compile_top_stmt (zend_compile.c:8453) ==58394== by 0x7A8F52: zend_compile (zend_language_scanner.c:614) ==58394== by 0x7AA807: compile_string (zend_language_scanner.c:779) ==58394== LEAK SUMMARY: ==58394== definitely lost: 3,216 bytes in 134 blocks ==58394== indirectly lost: 400 bytes in 12 blocks ==58394== possibly lost: 2,947,178 bytes in 23,784 blocks ==58394== still reachable: 1,834,952 bytes in 14,037 blocks ==58394== of which reachable via heuristic: ==58394== stdstring : 34,756 bytes in 937 blocks ==58394== newarray : 488 bytes in 1 blocks ==58394== suppressed: 0 bytes in 0 blocks ``` ``` 请问以上这个是php自己本身的内存泄露吗, 和swoole有关系吗, 但我这线上实际内存泄露很严重, 命令写的不对还是那里没弄对吗, 还是说我的php代码本身有些地方如static变量没处理好导致的内存泄露valgrind检测不出来? ```
发布于5年前 · 21 次浏览 · 来自
提问
lg430
### 问题描述 线上服务worker内存上涨非常快, 千万以上请求量后内存仍然在涨 ### Swoole版本,PHP版本,以及操作系统版本信息 ``` PHP 7.4.12 (cli) (built: Nov 11 2020 13:35:32) ( NTS ) Copyright (c) The PHP Group Zend Engine v3.4.0, Copyright (c) Zend Technologies with Zend OPcache v7.4.12, Copyright (c), by Zend Technologies ``` ``` swoole Swoole => enabled Author => Swoole Team <team@swoole.com> Version => 4.5.7 Built => Nov 11 2020 13:37:00 coroutine => enabled epoll => enabled eventfd => enabled signalfd => enabled cpu_affinity => enabled spinlock => enabled rwlock => enabled openssl => OpenSSL 1.1.1h 22 Sep 2020 http2 => enabled zlib => 1.2.7 mutex_timedlock => enabled pthread_barrier => enabled futex => 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 ``` ``` centos 7.3 kernel 4.18.20 OpenSSL 1.1.1h 22 Sep 2020 ``` ### valgrind.log ``` $ USE_ZEND_ALLOC=0 valgrind --leak-check=full --show-leak-kinds=all --error-limit=no --log-file=valgrind.log php script.php ==58394== 24 bytes in 1 blocks are definitely lost in loss record 4,224 of 15,037 ==58394== at 0x4C27BE3: malloc (vg_replace_malloc.c:299) ==58394== by 0x7E5BAF: zend_register_functions (zend_API.c:2297) ==58394== by 0x7E6D65: do_register_internal_class (zend_API.c:2708) ==58394== by 0x807FA1: zend_register_weakref_ce (zend_weakrefs.c:194) ==58394== by 0x7F4BF6: zm_startup_core (zend_builtin_functions.c:313) ==58394== by 0x7E4E44: zend_startup_module_ex (zend_API.c:1860) ==58394== by 0x7E4EDB: zend_startup_module_zval (zend_API.c:1875) ==58394== by 0x7F1802: zend_hash_apply (zend_hash.c:1812) ==58394== by 0x7E5189: zend_startup_modules (zend_API.c:1986) ==58394== by 0x7832E5: php_module_startup (main.c:2332) ==58394== by 0x8640DC: php_cli_startup (php_cli.c:410) ==58394== by 0x4627D3: main (php_cli.c:1326) -- ==58394== 24 bytes in 1 blocks are definitely lost in loss record 4,225 of 15,037 ==58394== at 0x4C27BE3: malloc (vg_replace_malloc.c:299) ==58394== by 0x7B98C8: __zend_malloc (zend_alloc.c:2976) ==58394== by 0x7C691E: zend_compile_params (zend_compile.c:5347) ==58394== by 0x7CF21B: zend_compile_func_decl (zend_compile.c:6050) ==58394== by 0x7C7DE4: zend_compile_expr (zend_compile.c:8716) ==58394== by 0x7CAD89: zend_compile_args (zend_compile.c:3059) ==58394== by 0x7CB197: zend_compile_call_common (zend_compile.c:3136) ==58394== by 0x7CBABD: zend_compile_static_call (zend_compile.c:4032) ==58394== by 0x7CAC30: zend_compile_var (zend_compile.c:8744) ==58394== by 0x7C7D14: zend_compile_expr (zend_compile.c:8613) ==58394== by 0x7CE2BA: zend_compile_stmt (zend_compile.c:8577) ==58394== by 0x7CEFD6: zend_compile_stmt_list (zend_compile.c:5266) -- ==58394== 3,568 (3,168 direct, 400 indirect) bytes in 132 blocks are definitely lost in loss record 14,928 of 15,037 ==58394== at 0x4C27BE3: malloc (vg_replace_malloc.c:299) ==58394== by 0x7B98C8: __zend_malloc (zend_alloc.c:2976) ==58394== by 0x7C691E: zend_compile_params (zend_compile.c:5347) ==58394== by 0x7CF21B: zend_compile_func_decl (zend_compile.c:6050) ==58394== by 0x7CE5DB: zend_compile_stmt (zend_compile.c:8545) ==58394== by 0x7CEFD6: zend_compile_stmt_list (zend_compile.c:5266) ==58394== by 0x7CE43B: zend_compile_stmt (zend_compile.c:8489) ==58394== by 0x7CFD25: zend_compile_class_decl (zend_compile.c:6477) ==58394== by 0x7D14FE: zend_compile_top_stmt (zend_compile.c:8464) ==58394== by 0x7D14D6: zend_compile_top_stmt (zend_compile.c:8453) ==58394== by 0x7A8F52: zend_compile (zend_language_scanner.c:614) ==58394== by 0x7AA807: compile_string (zend_language_scanner.c:779) ==58394== 24 bytes in 1 blocks are definitely lost in loss record 4,224 of 15,037 ==58394== at 0x4C27BE3: malloc (vg_replace_malloc.c:299) ==58394== by 0x7E5BAF: zend_register_functions (zend_API.c:2297) ==58394== by 0x7E6D65: do_register_internal_class (zend_API.c:2708) ==58394== by 0x807FA1: zend_register_weakref_ce (zend_weakrefs.c:194) ==58394== by 0x7F4BF6: zm_startup_core (zend_builtin_functions.c:313) ==58394== by 0x7E4E44: zend_startup_module_ex (zend_API.c:1860) ==58394== by 0x7E4EDB: zend_startup_module_zval (zend_API.c:1875) ==58394== by 0x7F1802: zend_hash_apply (zend_hash.c:1812) ==58394== by 0x7E5189: zend_startup_modules (zend_API.c:1986) ==58394== by 0x7832E5: php_module_startup (main.c:2332) ==58394== by 0x8640DC: php_cli_startup (php_cli.c:410) ==58394== by 0x4627D3: main (php_cli.c:1326) -- ==58394== 24 bytes in 1 blocks are definitely lost in loss record 4,225 of 15,037 ==58394== at 0x4C27BE3: malloc (vg_replace_malloc.c:299) ==58394== by 0x7B98C8: __zend_malloc (zend_alloc.c:2976) ==58394== by 0x7C691E: zend_compile_params (zend_compile.c:5347) ==58394== by 0x7CF21B: zend_compile_func_decl (zend_compile.c:6050) ==58394== by 0x7C7DE4: zend_compile_expr (zend_compile.c:8716) ==58394== by 0x7CAD89: zend_compile_args (zend_compile.c:3059) ==58394== by 0x7CB197: zend_compile_call_common (zend_compile.c:3136) ==58394== by 0x7CBABD: zend_compile_static_call (zend_compile.c:4032) ==58394== by 0x7CAC30: zend_compile_var (zend_compile.c:8744) ==58394== by 0x7C7D14: zend_compile_expr (zend_compile.c:8613) ==58394== by 0x7CE2BA: zend_compile_stmt (zend_compile.c:8577) ==58394== by 0x7CEFD6: zend_compile_stmt_list (zend_compile.c:5266) -- ==58394== 3,568 (3,168 direct, 400 indirect) bytes in 132 blocks are definitely lost in loss record 14,928 of 15,037 ==58394== at 0x4C27BE3: malloc (vg_replace_malloc.c:299) ==58394== by 0x7B98C8: __zend_malloc (zend_alloc.c:2976) ==58394== by 0x7C691E: zend_compile_params (zend_compile.c:5347) ==58394== by 0x7CF21B: zend_compile_func_decl (zend_compile.c:6050) ==58394== by 0x7CE5DB: zend_compile_stmt (zend_compile.c:8545) ==58394== by 0x7CEFD6: zend_compile_stmt_list (zend_compile.c:5266) ==58394== by 0x7CE43B: zend_compile_stmt (zend_compile.c:8489) ==58394== by 0x7CFD25: zend_compile_class_decl (zend_compile.c:6477) ==58394== by 0x7D14FE: zend_compile_top_stmt (zend_compile.c:8464) ==58394== by 0x7D14D6: zend_compile_top_stmt (zend_compile.c:8453) ==58394== by 0x7A8F52: zend_compile (zend_language_scanner.c:614) ==58394== by 0x7AA807: compile_string (zend_language_scanner.c:779) ==58394== LEAK SUMMARY: ==58394== definitely lost: 3,216 bytes in 134 blocks ==58394== indirectly lost: 400 bytes in 12 blocks ==58394== possibly lost: 2,947,178 bytes in 23,784 blocks ==58394== still reachable: 1,834,952 bytes in 14,037 blocks ==58394== of which reachable via heuristic: ==58394== stdstring : 34,756 bytes in 937 blocks ==58394== newarray : 488 bytes in 1 blocks ==58394== suppressed: 0 bytes in 0 blocks ``` ``` 请问以上这个是php自己本身的内存泄露吗, 和swoole有关系吗, 但我这线上实际内存泄露很严重, 命令写的不对还是那里没弄对吗, 还是说我的php代码本身有些地方如static变量没处理好导致的内存泄露valgrind检测不出来? ```
赞
0
收藏
提问
分享
讨论
建议
公告
开发框架
CodeGalaxy
登录
后参与评论
评论
2020-11-12
鲁飞
可以用Swoole Tracker免费的内存泄漏检测工具看看 https://www.kancloud.cn/swoole-inc/ee-help-wiki/1941569
赞
0
回复
2020-11-12
lg430
回复
鲁飞
为何内存泄漏检测工具在/tmp/trackerleak稍微大点的情况下(如100M以上)就崩溃了呢... php -r "trackerAnalyzeLeak();" // 命令返回空 检测内存泄露可能需要较长时间, 所以日志文件会比较大, 有什么办法解决吗?
赞
0
回复
2020-11-12
鲁飞
回复
lg430
请求几次就能100M?
赞
0
回复
2020-11-13
lg430
回复
鲁飞
线上都是单机300qps以上, tcpcopy下来的流量几十秒就100M了, 请求几次没法确定内存泄露位置
赞
0
回复
2020-11-13
鲁飞
回复
lg430
那tracker是不行了,不支持那么大。 提issue吧,在swoole-src提问你内存泄漏的问题。 ``` USE_ZEND_ALLOC=0 valgrind --log-file=/tmp/valgrind.log php xxx.php ```
赞
0
回复
2020-11-13
郭新华
这个工具不能在线上用的 文章有说呀
赞
0
回复
2020-11-13
lg430
回复
郭新华
没有,是tcpcopy了一份线上机器流量到测试机, 在测试机使用的valgrind和swoole_tracker
赞
0
回复
2020-11-14
Rango
这些信息是正常的,这是 `PHP` 底层设计的问题,有些内存是在进程退出时释放的,并非运行时问题,可以忽略
赞
0
回复