PHP print_r $exception->getTrace() Fatal Error (Memory issue)

Viewed 128

I am using print_r($exception->getTrace()); and I get the below error. I want an answer why this error occures when using print_r. When using var_dump it does not occur.

https://www.php.net/manual/en/exception.gettrace.php

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 98570240 bytes) in

try {
}
catch (Exception $exception) {
$a = print_r($exception->getTrace()); //memory issue
var_dump($exception->getTrace()); //works fine
}

Update:

getTraceAsString() solved this issue in some cases.

1 Answers

if your exception comes from some infinite loop, the trace will be super big and the size can be very large (hundreds of mb).

This error occur because the trace is big, so depending of the type of error and the data it print, if your memory is not set to be big enought, you will have this type of error

EDIT:

The var_dump function displays structured information about variables/expressions including its type and value. Arrays are explored recursively with values indented to show structure. It also shows which array values and object properties are references.

The print_r() displays information about a variable in a way that's readable by humans. array values will be presented in a format that shows keys and elements. Similar notation is used for objects.

which mean your var_dump does go deeper in each sub-object, will print_r does, recursively.

Related