I'm suffering from the classic memory exhaustion with the error message
Fatal error: Allowed memory size of 1073741824 bytes exhausted (tried to allocate 11198913 bytes)
I've read about this being what it is: I'm using too much RAM memory. I also tried adding gc_collect_cycles
at the end of my loop with no luck and finally I read about setting variables to null and not unset. The problem is I'm not sure what I need to unset here.
Consider the following script
public function run_all() {
$accounts = $this->websiteSettings->all();
foreach ($accounts as $websiteAccount) {
try {
$this->run($websiteAccount);
} catch (Exception $e) {
$ticket = new Ticket();
$ticket->priority = Ticket::PRIORITY_HIGH;
$ticket->status = Ticket::STATUS_OPEN;
$ticket->summary = 'Error';
$ticket->message = $e->getMessage();
$ticket->create();
}
}
}
$this->run
returns boolean, but I don't care about it because this script is being invoked by Cron and whenever an error appears, I save a Ticket to be analyzed by someone. So I don't store the return anywhere. I know deep down $this->run()
uses somewhat a little bit too much of memory, but it only uses a max of 100MB ~ 200MB
per run. My problem is that this weekly script will run on about 100 records and for each time it runs, the RAM in use is not being cleaned.
Bottom line is: Is it possible for me to clean EVERYTHING that has been used by $this->run
at the end of the loop? If so, how?
Edit
Here is the run
method.
public function run(WebsiteSettings $websiteSettings) {
if (empty($websiteSettings->key)) {
// @TODO log this because we need to investigate why it was invoked at all
return false;
}
// Load Settings
$settings = $websiteSettings->get_website_setting($websiteSettings->website_id, 'express-code', true);
$has_express_enabled = ($settings && $settings->value);
// Start the Feed Gateway
$feedGateway = new feedGateway($websiteSettings->value, $websiteSettings->website_id, $has_express_enabled);
$feedGateway->run();
return true;
}