我試圖foreach通過數組中的許多千項,做一些操作並將一些值保存到mysql表。內存限制超過PHP foreach
但是,當我循環時,內存使用量會不斷增加,直到我用完php.ini中指定的內存爲止,這非常快。
我試過使用unset,將變量設置爲null並查看垃圾回收,但沒有任何影響。
有沒有一種更有效的方式來循環這些元素(即內存使用不會持續增長)。
下面是我正在做的一個簡單的例子。
foreach ($subscribers as $subscriber)
{
$member = new Member($subscriber['id']);
if ($member['id'] > 0)
{
$bulletin = Bulletin::getCustomBulletin($member['id']);
Bulletin::compileBulletin($member['email'], time(), $bulletin['title'], $bulletin['content']);
echo $member['email'] . "\n";
echo memory_get_usage() . "\n";
}
}
這將產生以下結果:
[email protected]
11336688
[email protected]
12043640
[email protected]
12749952
你的php.ini中的內存限制是多少? – webbiedave 2012-04-24 15:07:16
我已經將它從32MB移動到了512MB。我目前只在本地機器上進行測試。我知道在php.ini中做出的任何合理的選擇都會太小。 – 2012-04-24 15:10:26
您是否嘗試用僅返回所需詳細信息的靜態查找函數替換成員對象實例化?您甚至可以嘗試用靜態數組替換該行,並查看是否仍然存在問題。 – 2012-04-24 15:12:49