爲一個對象(或其它值)的存儲器,當沒有提及它在PHP過程的任何地方只能被釋放。在你的情況下,行$customer = null
只會將該對象的引用數量減少1,但不會達到零。
如果你考慮一個簡單的循環,這可能會變得更加清晰:
$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
可能會證實這一點。
@IMSoP確實... –
這是另一個[參考](http://ringsdorff.net/2009/07/23/guest-post-fix-for-memory-leaks-in-magento)我發現。它看起來像問題駐留在循環引用。 – osondoar
@osondoar如果您至少使用PHP 5.3(您現在應該知道),則循環引用將被垃圾收集器捕獲,儘管不是立即。但是,請參閱我的答案,爲什麼您的示例不會釋放非循環引用。 – IMSoP