Script memory usage, and displaying with PHP


I recently had a problem where a script written by someone else was stopping. There was not warning, error, or indication at all what the problem was. The script simply died as if the process ended.
After a bit of troubleshooting I found that the script was ending due to memory loss. So here is how I used PHP’s memory_get_usage() to troubleshoot through it.

Here is a chart I used for conversion, to simply display the numbers.

1 Byte = 8 Bit
1 Kilobyte = 1024 Bytes
1 Megabyte = 1048576 Bytes
1 Gigabyte = 1073741824 Bytes

Here is how I used echo to display the memory used in MB’s at different points in the script to troubleshoot.

echo "Memory Usage: " . (memory_get_usage()/1048576) . " MB \n";