Der Speicher für ein Objekt (oder einen anderen Wert) kann nur freigegeben werden, wenn es keine Verweise auf sie überall in der PHP-Prozess sind. In Ihrem Fall verringert die Zeile $customer = null
nur die Anzahl der Verweise auf dieses Objekt um eins, aber sie bewirkt nicht, dass sie Null erreicht.
Wenn Sie eine einfachere Schleife betrachten, kann dies klarer werden:
$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
}
Da Sie Objekte verwenden, gibt es einen zusätzlichen Effekt - das Objekt innerhalb der Schleife ändern können, ohne dass eine neue Kopie davon zu schaffen :
$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
in Ihrem Fall wird die ->load()
Methode erweitert vermutlich die Datenmenge, die in jedem der Mitglieder der $customersCollection
wiederum mehr Speicher für jede erfordern. Die Überprüfung von $customersCollection
vor und nach der Schleife würde dies wahrscheinlich bestätigen.
@IMSoP in der Tat ... –
Dies ist eine weitere [Referenz] (http://ringsdorff.net/2009/07/23/guest-post-fix-for-memory-leaks-in-magento) das habe ich gefunden. Es sieht so aus, als ob das Problem auf zirkulären Referenzen liegt. – osondoar
@osondoar Wenn Sie mindestens PHP 5.3 verwenden (was Sie jetzt sein sollten), werden zirkuläre Referenzen von einem Garbage Collector abgefangen, wenn auch nicht sofort. Sehen Sie sich jedoch meine Antwort an, warum Ihr Beispiel selbst nicht-zirkuläre Referenzen nicht freigibt. – IMSoP