One solution is to log the memory usage information to a log file or a database.
But this solution isn’t perfect either, because you will end up with tons of logs that you need to read and manage.
A smarter solution is to make PHP do the hard work.
Instead of printing or logging everything, just check whether the memory usage is too high.
If not, continue the script execution. Otherwise, send yourself a notification email together with useful debugging information.
This simple procedure removes the need for you to actively check your scripts.
In fact, the scripts themselves will tell you when there’s something wrong 😉
Much better, isn’t it?
So, how can this be done in practice?