php - 我的脚本中是否存在内存泄漏,或者是另一个罪魁祸首?

标签 php linux apache codeigniter memory-leaks

我最近从共享主机切换到专用主机,这给了我更多的监控/控制。我一直在尝试调试自从切换之前就遇到的问题,内存使用率非常高。我想我已经将其范围缩小到一个特定的脚本,该脚本是对 Instagram feed/api 的订阅。它在 codeIgniter 框架中工作。

这是我的流程的屏幕截图。请注意非常高的 httpd 内存值 enter image description here

这是我在 codeIgniter 中的 Controller

class Subscribe extends CI_Controller {

    function __construct() {
        parent::__construct();
        $this->instagram_api->access_token = 'hidden';
    }
    function callback()
    {

    //echo anchor('logs/activity.log', 'LOG');
$min_id = ''; 
$next_min_id = '';      

$this->load->model('Subscribe_model');
$min_id = $this->Subscribe_model->min_id();

echo $min_id;

    $pugs = $this->instagram_api->tagsRecent('tagg','',$min_id);
    if($pugs){
    if (property_exists($pugs->pagination, 'min_tag_id')) {
            $next_min_id = $pugs->pagination->min_tag_id;
        }   
    foreach($pugs as $pug) {
        if(is_array($pug)) {     
            foreach($pug as $media) { 
                $url = $media->images->standard_resolution->url;
                $m_id = $media->id;
                $c_time = $media->created_time;
                $user = $media->user->username;
                $filter = $media->filter;
                $comments = $media->comments->count;
                $caption = $media->caption->text;
                $link = $media->link;
                $low_res=$media->images->low_resolution->url;
                $thumb=$media->images->thumbnail->url;
                $lat = $media->location->latitude;
                $long = $media->location->longitude;
                $loc_id = $media->location->id;
                $date = new DateTime('2000-01-01', new DateTimeZone('Pacific/Nauru'));

                $data = array(
                   'media_id' => $m_id,
                   'min_id' => $next_min_id,
                   'url' => $url, 
                   'c_time' => $c_time,
                   'user' => $user,
                   'filter' => $filter,
                   'comment_count' => $comments,
                   'caption' => $caption,
                   'link' => $link, 
                   'low_res' => $low_res,
                   'thumb' => $thumb,
                   'lat' => $lat,
                   'long' => $long,
                   'loc_id' => $loc_id,
                );
                $this->Subscribe_model->add_pug($data);

            }

        }

    }
    }

这是模型......

class Subscribe_model extends CI_Model {

    function min_id(){

        $this->db->order_by("c_time", "desc");      
        $query = $this->db->get("pugs");

        if ($query->num_rows() > 0)
        {
           $row = $query->row(); 
           $min_id = $row->min_id;
           if(!$min_id){
            $min_id ='';
           }
        }   

        return $min_id;

    }

    function add_pug($data){

        $query = $this->db->get_where('pugs', array('media_id'=>$data['media_id']));
        if($query->num_rows() > 0){
            return FALSE;   
        }else{
            $this->db->insert('pugs', $data);   
        }

    }
}

//============================编辑==========================//

我已经将一些服务转换为 fast-cgi,它似乎显着降低了我的内存使用量,但我注意到 CPU 有所增加。我希望切换到专用服务器会少得多的麻烦并使事情变得容易得多,但到目前为止这一直是一场噩梦。恐怕我已经贪多嚼不烂了。

我的另一个担心是向服务器添加更多域名。这是否会添加一个新进程,该进程将像上一个图像中运行的多个 php-cgi 一样运行得非常高?

这是我最近的输出... enter image description here enter image description here

最佳答案

为了确保没有真正的内存泄漏,请尝试只在服务器上运行 httpd/myslqd (killall Xorg/telinit 3),然后停止上述两个服务。记下 free|grep Mem |sed 's/\([^0-9]*[^\]*\)\{3\}\([^\]*\).*/\1/' 的输出。这是 RAM 的 X 个可用字节。现在,启动 httpd/mysqld 服务并让它们运行数百个请求。停止服务并再次记下数字,重复直到对中值结果感到满意。

httpd 消耗大量 RAM 的情况并不罕见。 mysqld 也在内存中缓存。这只是因为,如果连续多次(连续)遇到相同的请求,则静态缓存已准备好缓冲并且可以使用。

对于 PHP 来说,类是预编译的,一旦系统在第一次编译后需要它,它就不必逐行解释脚本,它将有一个字节码编码的对象可以使用。如果 fstat.mtime > bytecode.mtime.. 则编译当然会重新编译..

您可以使用以下命令分析非交换类型的实际内存使用情况:

ps -ylC httpd --sort:rss

提供静态文件的子进程大小约为2-3M。对于PHP等动态内容,可能在15M左右

要配置 apache 如何设置工作线程,这些参数在 httpd.conf 中有效:

StartServers, 
MaxClients, 
MinSpareThreads, 
MaxSpareThreads, 
ThreadsPerChild, 
MaxRequestsPerChild 

检查此链接:http://www.howtoforge.com/configuring_apache_for_maximum_performance

Section 3.5:

The MaxClients sets the limit on maximum simultaneous requests that can be supported by the server. No more than this much number of child processes are spawned. It shouldn't be set too low such that new connections are put in queue, which eventually time-out and the server resources are left unused. Setting this too high will cause the server to start swapping and the response time will degrade drastically. Appropriate value for MaxClients can be calculated as: MaxClients = Total RAM dedicated to the web server / Max child process size

可在此处进行 Apache 性能调整 http://httpd.apache.org/docs/2.0/misc/perf-tuning.html ,跳至有关进程创建的部分,以获取有关上述配置选项的更多信息。

关于php - 我的脚本中是否存在内存泄漏,或者是另一个罪魁祸首?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11076410/

相关文章:

php - 仅将不存在的数据从 CSV 导入数据库

linux - 在fish shell中定义别名

linux - R 包含目录为空

php - 在 X 按钮下添加自定义文本,用于从购物车中删除商品

php - 标准使用 session ID 显示相关信息

php - phpBB 500内部服务器错误,无日志输出

apache - 虚拟主机和 htaccess 的重定向过多

apache - 启动 apache 时出错,httpd : Configuration error: More than one MPM loaded

php - Google Calendar Gdata 旧的重复事件已删除事件仍然显示,事件 ID 以 Z 结尾

Linux 套接字描述符