Linux上php服务的高内存使用率

时间:2018-05-24 05:34:19

标签: php linux

每天有4GB的内存使用量,而且根本不能清理

拥有以下php脚本:

while(1){
    sleep(15);
    $data = // get data

    foreach ($data as $d){
        //get the db status
        // make api call in same ip to update server 
        // write log
    }
}

service linux code (via docker):

[program:cron]
command=/bin/bash -c "exec cron -f"
autostart=true
autorestart=true
startretries=3

可以做些什么来防止这种情况?它应该是一个样本服务

2 个答案:

答案 0 :(得分:-1)

看起来您正在使用supervisord,为什么不将命令换出而不是通过cron运行:

[program:phpscript]
command=/path/to/php /path/to/script.php
autostart=true
autorestart=true
startretries=3

答案 1 :(得分:-1)

很可能你有一些数据结构就像一个数组,它会在每次运行时附加更多数据并且永远不会被释放。你的循环中也有可能以某种方式产生循环,而不是立即收集。

您可以在gc_collect_cycles();循环后添加foreach来快速检查。如果这解决了问题,那很好。您可以在https://secure.php.net/manual/en/features.gc.collecting-cycles.php

了解有关参考周期的更多信息

否则,你应该看一下分析器,看看究竟剩下什么。请参阅PHP memory profiling和其他类似问题。