Sterling Duchess
Sterling Duchess

Reputation: 2080

Eloquent eating up memory - Allowed memory size of 134217728 bytes exhausted

I tried working today however I got interrupted by this error:

[Tue Aug 05 09:08:48 2014] [error] [client 93.103.209.208] PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 130968 bytes) in /var/www/dev/vendor/laravel/framework/src/Illuminate/Database/Eloquuent/Model.php on line 643

The controller/method that results in the error:

public function show($id)
{
 return $this->layout->content = View::make('users.show', array(
  'user' => User::find($id)
 ));
}

However if I replace User::find($id) with DB::table('users')->where('id', '=', $id)->first(); it works like a charm. What is going on here with Eloquent ?

I tried increasing my memory_limit in php.ini for apache & cli to 200-230M but it did not help. I also tried setting it to -1 to be unlimited just to see if it would work but no then i simply get no response from the server. And yes I did restart apache after every change.

CAUSE

The problem was a result of having a relation method in my User model to it self. Basically in my User class I had User { public function user() { return $this->hasOne('User'); } } which created a loop.

Upvotes: 1

Views: 2411

Answers (2)

James C
James C

Reputation: 14149

Just dissecting the error message - you have a memory limit of 128MB setup for PHP, the line in question is trying to allocate an additional ~131KB

The variables using the rest of the memory might not be related to this function, it could well just be the case that this part of the code looking for its 131KB is just consistently pushing you over the 128MB limit.

I'd use some logging mechanism to record the return value of memory_get_usage() at various points in your code. This should hopefully give you some indication of when the memory's being allocated and allow you to figure out how to optimise the code.

Upvotes: 2

Robin
Robin

Reputation: 132

Change your code to the following:

...
return $this->layout->content = View::make('users.show')->with(array('user' => User::find($id)));
...
// or WITHOUT return
$this->layout->content = View::make('users.show')->with(array('user' => User::find($id)));

Upvotes: 0

Related Questions