0

Consider the following loop:

// Determine the final state of orders.
$orders = Mage::getModel('sales/order')
    ->getCollection()
    ->addAttributeToFilter('entity_id', array_keys($orderIDs));
foreach ($orders as $order) {
    // Tally all of an orders items
    $itemIDs = [];
    $iCollection = $order->getItemsCollection()
        ->addAttributeToFilter('product_type', 'bundle')
        ->addAttributeToSelect('entity_id');
    foreach ($order->getItemsCollection() as $item) {
        $itemIDs[] = (int)$item->getItemId();
    }

    // Tally the order items we've already dealt with
    $batchItemIDs = [];
    $batchItems = Mage::getModel('callcenter_fulfillment/batch_item')
        ->getCollection()
        ->addFieldToFilter('item_id', $itemIDs)
        ->addFieldToSelect('entity_id');
    foreach ($batchItems as $batchItem) {
        $batchItemIDs[] = (int)$batchItem->getItemId();
    }

    $order->setData('state', 'complete');
    if ($iCollection->getSize() === $batchItems->getSize()) {
        $order->setData('status', 'complete');
    } else {
        $order->setData('status', 'partially_completed');
    }
    $order->save();
}

This loop leaks memory. I have dissected the memory leakage, and I have found $iCollection allocates 95k of memory, $order->setData(whatever) at the bottom allocates 18k, and $order->save() at the bottom by itself allocates around 53k of memory. This is according to memory_get_usage() logged by each part of the code.

Now, I don't have a problem with the amount of memory that's being allocated. I understand that ORM's have overhead. However, as far as I can tell, almost none of this memory is being freed on subsequent loops, so memory usage inches higher and higher until the site finally blows up by hitting the 128 megabyte limit.

Why isn't memory being freed here? Is there something I'm overlooking?

0

I would suggest calling the clear() method on the order item collection object $iCollection or unsetData on the $order object once you're doing performing "work" on it.

You may also look at calling the getAllIds method on the item collection since the code in your question does not utilize the order item objects aside from their IDs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.