We use cookies to improve your experience. No personal information is gathered and we don't serve ads. Cookies Policy.

ExpressionEngine Logo ExpressionEngine
Features Pricing Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University
Log In or Sign Up
Log In Sign Up
ExpressionEngine Logo
Features Pro new Support Find A Developer
Partners Upgrades
Blog Add-Ons Learn
Docs Forums University Blog
  • Home
  • Forums

PHP Memory Exhausted

Development and Programming

manish's avatar
manish
26 posts
3 years ago
manish's avatar manish

I am getting a PHP memory exhausted error even though the memory_limit is set to “-1” using “ini_set(“memory_limit”,”-1”); Tracing down the exact location where the issue is, I get to the “getAllMembers” function. There are more than 100k users in the site where this error is occurring. Error occurs when the super admin logs in to control panel and navigates to “Tool”->”utilities” area which has the “communication” or “email” form.

Could you suggest what could be going wrong?

Thanks.


File: system/ee/ExpressionEngine/Model/Role/Role.php Original Code: public function getAllMembers() { $members = array_replace($this->Members->indexBy(‘member_id’), $this->PrimaryMembers->indexBy(‘member_id’)); foreach ($this->RoleGroups as $role_group) { foreach ($role_group->Members as $member) { $members[$member->member_id] = $member; } } return new Collection($members); }

Altered function (obviously, the functionality is changed… ) that gets rid of the error: public function getAllMembers() { if ($this->role_id!=5) $members = array_replace($this->Members->indexBy(‘member_id’), $this->PrimaryMembers->indexBy(‘member_id’)); else $members = $this->Members->indexBy(‘member_id’); // $members = $this->PrimaryMembers->indexBy(‘member_id’); // using this code causes out of memory error

foreach ($this->RoleGroups as $role_group) {
    foreach ($role_group->Members as $member) {
        $members[$member->member_id] = $member;
    }

} return new Collection($members); }

       
manish's avatar
manish
26 posts
3 years ago
manish's avatar manish

Sorry for the garbling of text. I tried to edit, but the edit icon takes to a page where I can only do a new post. Here is the code again.

File: system/ee/ExpressionEngine/Model/Role/Role.php Original Code:

public function getAllMembers() { 
  $members = array_replace($this->Members->indexBy(‘member_id’), $this->PrimaryMembers->indexBy(‘member_id’)); 
  foreach ($this->RoleGroups as $role_group) { 
    foreach ($role_group->Members as $member) { 
      $members[$member->member_id] = $member; 
    } 
  } 
  return new Collection($members); 
}

Altered function (obviously, the functionality is changed… ) that gets rid of the error:

public function getAllMembers() { 
  if ($this->role_id!=5) 
    $members = array_replace($this->Members->indexBy(‘member_id’), $this->PrimaryMembers->indexBy(‘member_id’)); 
  else 
    $members = $this->Members->indexBy(‘member_id’); 
// $members = $this->PrimaryMembers->indexBy(‘member_id’); // using this code causes out of memory error

  foreach ($this->RoleGroups as $role_group) {
    foreach ($role_group->Members as $member) {
       $members[$member->member_id] = $member;
    }
  } 
  return new Collection($members); 
}
       
Andy McCormick's avatar
Andy McCormick
183 posts
3 years ago
Andy McCormick's avatar Andy McCormick

manish, just for clarification, what version of EE is this?

       
manish's avatar
manish
26 posts
3 years ago
manish's avatar manish

Software versions:

Expression Engine v6.0.3. PHP 7.4.19 Apache/2.4.37 CentOS 8

Please let me know if any additional information is needed.

       
Robin Sowell's avatar
Robin Sowell
13,158 posts
3 years ago
Robin Sowell's avatar Robin Sowell

Nice job of debugging- see this pull request for the fix.

       
manish's avatar
manish
26 posts
3 years ago
manish's avatar manish

Hi, I have tried this change but it does not fix the issue. I still get to the “out of memory” error, which is visible only if debugging is enabled and super admin is logged in.

       
Robin Sowell's avatar
Robin Sowell
13,158 posts
3 years ago
Robin Sowell's avatar Robin Sowell

Hrm- how much memory do you have?

       
manish's avatar
manish
26 posts
3 years ago
manish's avatar manish

Hi Robin, here is the error message I get. I have allocated 1GB RAM to each PHP process, which is a way more than necessary. If I allocated more, it will take that up also.

Fatal error: Allowed memory size of 1073741824 bytes exhausted (tried to allocate 20480 bytes) in <base-path>/system/ee/legacy/database/DB_result.php on line 84

It seems that the issue is that the code is trying to load all 125k+ rows from members table and then organising the data in RAM… this is likely what is causing the memory to be exhausted.

Thank.

       
manish's avatar
manish
26 posts
3 years ago
manish's avatar manish

I have tried with 2G allocation to each PHP and the script times out.

The issue is with this call: $members = $this->PrimaryMembers->indexBy(‘member_id’);

This “$this->PrimaryMembers” is where things go south. In the update code (in pull request), there is a similar code “$role->PrimaryMembers->” where the code is failing. The fix undoes the “workaround” that is mentioned in the original posting.

Thanks.

       

Reply

Sign In To Reply

ExpressionEngine Home Features Pro Contact Version Support
Learn Docs University Forums
Resources Support Add-Ons Partners Blog
Privacy Terms Trademark Use License

Packet Tide owns and develops ExpressionEngine. © Packet Tide, All Rights Reserved.