mwal
mwal

Reputation: 3113

Pinpoint exact cause of PHP out of memory error ( ...it was var_dump() )

I've been getting the out of memory error occasionally for a few months now on an app that has been in continuous development & has about 20 users so far.

It didn't seem to have any negative repurcussions I could detect, but finally I have found the culprit.

I don't know how but I had a default 404 error view into which strange code had found its way, I'm not sure how I did that. This default 404 error view is called by the framework automatically if one uses findOrFail() and the db does not find a record. When a page was not available in my app (which is as it should be, as some content can be published/unpublished), this error view was triggering.

The weird code was:

<!-- <div><?php var_dump($exception); ?></div> -->
<div class="text">{{$exception->getMessage()}}</div>

Totally weird I know.

Firstly, the html comments, since it is a blade file, do not apply, so the var_dump was being called despite being 'commented out'.

So I replaced it with this:

{{ var_dump($exception) }}

and the out of memory error (and the 500 error in the browser) can be reliably reproduced.

Removing it, and the 404 view renders fine.

Replacing it with {{ dd($exception) }} works fine too - I get a render of the trace.

So, why does the var_dump line cause an out of memory error?

What should I do to investigate this further?

I'm on Laravel 5.3

Upvotes: 1

Views: 735

Answers (1)

OptimusCrime
OptimusCrime

Reputation: 14863

This most likely happens because the content of the $exception variable is big. When you dump the variable, PHP will attempt to convert it to a string representation. This uses a lot of memory.

I don't see why you would need to output the entire exception object. You most likely just need the message (which you fetch by $exception->getMessage(). The rest is just backtrace and references to related objects and instances.

Upvotes: 1

Related Questions