オブジェクト (またはその他の値) のメモリは、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
ループの前後を調べると、おそらくこれを確認できます。