0

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

}

3 Answers 3

1

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
}
Sign up to request clarification or add additional context in comments.

Comments

1

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();
   // ...
}

2 Comments

It changed the memory usage from 9855568 to 6885536 but i think i need a litle more than that Superb first step
10MB memory usage is not going to work for a lot of Magento stuff. You should increase the memory_limit php configuration variable to something like 512MB
0

How large is $_productCollection? You could try getting the products in batches or removing the product from the collection at the end of the for-loop.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.