Memory issue when updating all entries from a channel (allowed memory size of X bytes exhausted (tried to allocate Y bytes)) I'm trying to do a stock update for all entries within a channel with a cronjob once a day, and I can't achieve it, because I always get Allowed memory size of X bytes exhausted (tried to allocate Y bytes).

Entries are around 2000 and the server is an EC2 medium instance. The cron I'm using is:

0 9 * * * wget -O /home/ubuntu/cronStock http://domain.com/actions/garp/cron/changeStock


I will try to create a simpler script without all the loaders Craft calls in a controller, but I'm not sure it will work.

Code:

public function actionChangeStock()
{
    foreach ($this-&gt;getAllFromChannel('productVariants', 10) as $key =&gt; $entry) {

        //$entry = $this-&gt;getElementByField("3081001100390", ElementType::Entry, null, "variantSku");
        //$entry-&gt;enabled = false;
        $entry-&gt;getContent()-&gt;setAttributes(array(
            'variantStock'  =&gt; $this-&gt;getStockFromGarp($entry-&gt;variantSku)
        ));
        $success = craft()-&gt;entries-&gt;saveEntry($entry);

        echo $entry-&gt;variantSku.': '.$entry-&gt;variantStock.'&lt;br&gt;';
    }
    die();
}

protected function getAllFromChannel($channel, $limit = null) {
    $criteria = craft()-&gt;elements-&gt;getCriteria(ElementType::Entry);
    $criteria-&gt;section = $channel;
    $criteria-&gt;limit = $limit;

    return $criteria-&gt;find();
}


How can I fix this problem?
If you just want to re-save a large number of entries without modifying them, you can run a ResaveElements task, which takes some basic parameters and re-saves all the entries that match them.

You can do that like this:

// Queue up a new ResaveElements task
craft()-&gt;tasks-&gt;createTask('ResaveElements', null, array(
    'elementType' =&gt; ElementType::Entry,
    'criteria'    =&gt; array('section' =&gt; 'mySectionHandle', 'limit' =&gt; null, 'status' =&gt; null, 'localeEnabled' =&gt; null)
));

// Start running tasks
craft()-&gt;tasks-&gt;runPendingTasks();