Memory leak in Magento / Zend Framework

When I run this simple script, I get the output published below. This makes me think there is a memory leak in my code or in the Zend Framework / Magento stack. This issue occurs when repeating any Magento collection. Is there something that is missing or something is wrong?

Script:

$customersCollection = Mage::getModel('customer/customer')->getCollection();

foreach($customersCollection as $customer) {
  $customer->load();
  $customer = null;
  echo memory_get_usage(). "\n";
}

Conclusion:

102389104
102392920
...
110542528
110544744
+5
source share
5 answers

Your problem is that you send rather expensive requests with each iteration, when you can load the necessary data through collection requests:

$collection = Mage::getResourceModel('customer/customer_collection')->addAttributeToSelect('*');

, . , - customer_load_before customer_load_after ( ), .

: osonodoar (/ /customer_collection)

+7

( ) , PHP. $customer = null , .

, :

$test = array('a' => 'hello');
foreach ( $test as $key => $value )
{
    // $value points at the same memory location as $test['a']
    // internally, that "zval" has a "refcount" of 2

    $value = null;
    // $value now points to a new memory location, but $test['a'] is unnaffected
    // the refcount drops to 1, but no memory is freed
}

, - , :

$test = array('a' => new __stdClass);
// $test['a'] is an empty object

foreach ( $test as $key => $value )
{
    // $value points at the same object as $test['a']
    // internally, that object has a "refcount" of 2

    $value->foo = "Some data that wasn't there before";
    // $value is still the same object as $test['a'], but that object now has extra data
    // This requires additional memory to store that object

    $value = null;
    // $value now points to a new memory location, but $test['a'] is unnaffected
    // the refcount drops to 1, but no memory is freed
}

// $test['a']->foo now contains the string assigned in the loop, consuming extra memory

->load(), -, $customersCollection , . $customersCollection , , .

+3

-, unset ($ variable) $variable = null. , , , .

-, PHP , - , PHP- , , , , , , . , .

: , , - , , , .

0

- exec exec , .

, exec, .

, , , .

0

@benmarks , load() - .

$customer- > load() , $customersCollection, .

, load() - , , GC , .

$customersCollection = Mage::getModel('customer/customer')->getCollection();

foreach($customersCollection as $customer) {
    $customerCopy = Mage::getModel('customer/customer')->load($customer->getId());

    //Call to $customerCopy methods

    echo memory_get_usage(). "\n";
}
0
source

All Articles