I am creating a script to simulate a hockey game. All games will be simulated once per day. However when I run my simulator, after about 50 game simulations, I get an error that says
PHP Fatal error: Allowed memory size of 268435456 bytes exhausted
I tried using the unset() function to clear the memory after each game simulates but it doesn't seem to help. What can I be missing here? Should I be using some kind of destructor or are there any other php functions that can help clean up memory?
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.
}