我正在开发一个循环/迭代超过10,000次的PHP脚本:
foreach ($array_with_items as $item) {
// Instantiate the object
$obj_car = new CarAds($puk, 'ENG', '5');
$obj_car->detail1 = "Info about detail1";
$obj_car->detail2 = "Info about detail2";
$obj_car->detail3 = "Info about detail3";
$obj_car->detail4 = "Info about detail4";
// Saves to the database
$obk_car->save;
}
Run Code Online (Sandbox Code Playgroud)
当我运行此代码时,我的机器内存不足.在这个foreach循环中,我该怎么做才能清理内存?
您将$ Car_s对象实例化为$ array_with_items项目计数.每一个都分配内存.
在save()方法之后,您应该使用unset()函数释放该对象:
// Saves to the database
$obj_car->save;
// Unset unneeded object
unset($obj_car);
Run Code Online (Sandbox Code Playgroud)
您可以使用memory_get_usage()检查内存消耗(请参阅http://php.net/manual/en/function.memory-get-usage.php)
| 归档时间: |
|
| 查看次数: |
2456 次 |
| 最近记录: |