简体   繁体   English

PHP脚本耗尽内存

[英]Php script running out of memory

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 . 您好我试图编写一个将从远程服务器导入预告片的功能代码将我们的magento商店中的产品与预告片服务器上的预告片匹配。 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>\n";
    $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>\n";
            //echo memory_get_peak_usage() . "<br>\n";

        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>\n";

} }

You could reuse the same model instance in the loop: 您可以在循环中重用相同的模型实例:

foreach($_productCollection as $product)
{
    $product->load($product->getId());
    // ...

    $product->clearInstance();
}

or even better only load the stuff you need in the collection 或者甚至更好地只加载你在集合中需要的东西

$_productCollection = Mage::getModel('catalog/product')->getCollection()
      ->addAttributeToFilter('attribute_set_id', array('eq' => '9'))
      ->addAttributeToSelect(array('fakta_id', 'main_trailer'));

and then loop through without needing to reload the product: 然后循环,无需重新加载产品:

foreach($_productCollection as $product)
{
   $attributeValue = $thispro->getFaktaId();
   // ...
}

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. 你只需要他们的ID。

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
}

The following article describes about different ways to prevent running php out of memory. 下面的文章介绍了防止运行php内存不同的不同方法。

http://v1.srcnix.com/2010/02/10/7-tips-to-prevent-php-running-out-of-memory/ http://v1.srcnix.com/2010/02/10/7-tips-to-prevent-php-running-out-of-memory/

How large is $_productCollection? $ _productCollection有多大? You could try getting the products in batches or removing the product from the collection at the end of the for-loop. 您可以尝试批量获取产品,或者在for循环结束时从集合中删除产品。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM