dow5001 2014-02-20 09:02
浏览 44
已采纳

PHP脚本耗尽内存

Hi im trying to code a funktion that will import trailers from a remote server The code is matching product in our magento store with the trailers on the trailer server . The problem is that the script eating up all memory. is there any way of optimize this to not consume memory .

here are my function

    function getTrailers() {

    echo "<pre>
";
    $this->_acquireLock();

    $_productCollection = Mage::getModel('catalog/product')->getCollection()

      ->addAttributeToFilter('attribute_set_id', array('eq' => '9'));
     //->addAttributeToFilter('Sku', array('eq' => '843594'));


    $this->_logLine(sprintf("Starting import of Trailers" ));
    $i=0;
        $server = "http://trailer.server.com/trailers";
        foreach($_productCollection as $product) {
        $thispro  =  Mage::getModel('catalog/product')->load($product->getId());
        $attributeValue = $thispro->getFaktaId();

            //echo memory_get_usage() . "<br>
";
            //echo memory_get_peak_usage() . "<br>
";

        if($thispro->getMainTrailer()=="") {
            if($attributeValue != "") {
                $im = $attributeValue.".mp4";
                    $url = $server.$im;


        $exist = $this->file_exists_remote($url);


        if($exist) {
            $i++;
            $product->setMainTrailer($url);
            $product->save();
        } else {

            }
        }
    }
}
    $this->_logLine(sprintf("Imported %d Trailers...", $i));
    $this->_releaseLock();

    echo "</pre>
";

}

  • 写回答

4条回答 默认 最新

  • douzhou7656 2014-02-20 09:42
    关注

    Your main problem is that you keep collection of products you never use. Actually you don't need these products. You only need their IDs.

    So you have to edit your code like this:

    function getIds() {
        return Mage::getModel('catalog/product')->getCollection()
            ->addAttributeToFilter('attribute_set_id', array('eq' => '9'))
            ->getAllIds();
    }
    
    ...
    foreach(getIds() as $id) {
        $thispro = Mage::getModel('catalog/product')->load($id);
        ...
        // Further you have to replace all $product occurences with $thispro
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(3条)

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog