0

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;
}
Community
  • 1
  • 1
Marco Aurélio Deleu
  • 4,279
  • 4
  • 35
  • 63
  • What does `$this->run()` do? Does it create/instantiate anything? Are those things it creates being cleared once it's done using them? – Benjam Aug 02 '16 at 19:35
  • @Benjam it creates a local variable (inside run function) that instantiates other objects and run other methods. But the `run` method is also called in other places and it works great (when being called, just 1 website is executed). I would like to try and settle this inside `run_all` method so I don't change anything on what is already working. My confusion is to why things on `$this->run` is not being automatically cleared after the method is done on each iteration. – Marco Aurélio Deleu Aug 02 '16 at 19:43
  • @Benjam I added the `run` method to the question. – Marco Aurélio Deleu Aug 02 '16 at 19:49
  • Before the `return true;` in the run method, try either unsetting `$feedGateway` or setting it to `false` or `null`. – Benjam Aug 03 '16 at 06:32
  • @Benjam I dis with no luck. – Marco Aurélio Deleu Aug 03 '16 at 12:06
  • I did it with no luck * – Marco Aurélio Deleu Aug 03 '16 at 12:33

0 Answers0