我正在创建一个模拟曲棍球比赛的脚本。所有游戏每天都会模拟一次。然而,当我运行我的模拟器时,在大约50次游戏模拟之后,我收到一个错误,上面写着
PHP致命错误:允许的内存大小为268435456字节耗尽
我尝试使用unset()函数在每个游戏模拟后清除内存,但似乎没有帮助。我在这里可以缺少什么?我应该使用某种析构函数还是有其他任何可以帮助清理内存的PHP函数?
class GameSimulatorHelper extends Model
{
public function run()
{
// Get all the games from the database.
$games = Game::all();
// Simulate each game.
foreach ($games as $g) {
$sim = new GameSimulator();
$sim->run($g);
unset($sim);
}
}
}
class GameSimulator extends Model
{
public $homeGoals = 0;
public $awayGoals = 0;
// A bunch of other class variables here.
public function run($game)
{
$this->simulate($game);
// This function just resets all the class variables for the next game to be simulated.
$this->resetSimulator();
echo 'MEMORY USAGE: '.memory_get_usage(true) . "\n";
}
public function simulate()
{
$maxPeriods = 3;
for ($p=1; $p <= $maxPeriods; $p++) {
for ($i=1; $i <= 1200; $i++) {
// Do many things here like get and set data in database.
}
}
}
// Many other functions below.
}