【问题标题】:yii reduce php memory usage on actionyii 减少 php 的内存使用量
【发布时间】:2014-06-10 09:38:05
【问题描述】:

我试图查明为什么 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__);

【问题讨论】:

    标签: php yii memory-leaks


    【解决方案1】:

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

    $contactList = $obj->getContactList(array("condition"=>"(first_name is not null || (first_name is not null && last_name is not null)) && deleted = 0", "order"=> "名字"));

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-01-01
      • 2014-02-02
      • 2012-01-20
      • 2011-06-04
      • 2011-12-02
      • 2014-05-27
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多