I wanted a simple way to debug memory usage in a Laravel or simple PHP project. Something I can just drop into my code without packages, extensions or what not. This little helper does what I need.
- Create a helpers.php (see below) where ever you want.
- Add the helper file to your composer.json in the autoload/files section.
"autoload": {
"files": [
"app/Support/helpers.php",
]
},
Use the function in any PHP file before and after the code you want to debug like so.
\printMemory(); // before the script we want to check
$someClass = new SomeClass();
\printMemory(); // after we created our objects
$someClass = null;
unset($someClass);
\printMemory(); // after reset
The output will be something like this.