yii减少动作中的php内存使用量

时间:2014-06-10 09:38:05

标签: php yii memory-leaks

我试图找出为什么apache我的一些apache进程正在耗尽近250mb的内存。我有4gb分配给服务器和4GB交换。在一天的中途,它耗尽了交换空间。我相信这是用尽了内存的PHP。我发现以下行动产生96.5mb

public function actionView($id)
    {
        $model = $this->loadModel($id);
        $client=null;
        $obj =new GlobalController(); // preparing object

        if(strlen($model->cache(CACHE_TIMEOUT)->oRDERNO->CONTACTID) < 6){
            $client = Contacts::model()->cache(CACHE_TIMEOUT)->findByPk($model->cache(CACHE_TIMEOUT)->oRDERNO->CONTACTID);;
        }else{
            $dependency = new CDbCacheDependency('SELECT count(*) FROM sugarcrm6.contacts');
            $client = SugarContacts::model()->cache(CACHE_TIMEOUT,$dependency)->findByPk($model->cache(CACHE_TIMEOUT)->oRDERNO->CONTACTID);
        }

        // addressestransmittals
        $addressestransmittals = Addressestransmittals::model()->cache(CACHE_TIMEOUT)->findByPk($model->ORDERNO);
        //addressesconsultants
        $addressesconsultants = Addressesconsultants::model()->cache(CACHE_TIMEOUT)->findAll(array("condition"=>"ORDERNO = {$model->ORDERNO}"));

        $contactList = $obj->getContactList(array("condition"=>"(first_name is not null || (first_name is not null && last_name is not null) ) && deleted = 0", "order"=>"first_name"));
        Yii::log((memory_get_peak_usage(true))/1024/1024 . "MB",CLogger::LEVEL_INFO, __METHOD__);

        $this->render('view',array(
                'model'=>$model,
                'client'=>$client,
                'addressestransmittals'=>$addressestransmittals,
                'addressesconsultants'=>$addressesconsultants,
                'contactList'=>$contactList
        ));
    }

减少内存使用量的最佳方法是什么?

我将以下内容放在render之前,以记录内存

Yii::log((memory_get_peak_usage(true))/1024/1024 . "MB",CLogger::LEVEL_INFO, __METHOD__);

1 个答案:

答案 0 :(得分:2)

我发现罪魁祸首是以下一行

$ contactList = $ obj-&gt; getContactList(array(“condition”=&gt;“(first_name不为null ||(first_name不为空&amp;&amp; last_name不为null))&amp;&amp; deleted = 0“,”order“=&gt;”first_name“));

它正在查询sugarcrm数据库中的所有记录,似乎sugarcrm没有正确编入索引,用完了70MB