How to debug full RAM memory?

Just buy more RAM

In real OSs there is no such thing as “freeing” memory. Once an application was allocated memory it is owned by it and unlikely to be “returned” to the OS. So if you have an application which “leaks” memory, it is going to consume all available RAM at some point.

The way web servers usually handle request that require execution of PHP code is by spawning a process to handle it. Since spawning processes by itself is not a very efficient thing they spawn several on the web server start up, and communicate with them. How much memory can such a process use? as said above allocated memory is never returned to the OS, therefor the max amount of memory will be the max amount you allow PHP to use.

Developers of web servers are not total idiots, and they are aware of the memory allocation limitations, and to fight the problem each PHP handling process is being killed at some point usually based on user configured criteria. This reduces the chance that all those processes get to max memory together.

So you do not really need any special debugging to understand how did you get to consume all RAM. it is a simple equation of (number of PHP handling process) * (max memory PHP may allocate). If the result is way above you physical RAM you are in trouble.

How to fix? you can configure the web server to have less PHP handlers. This will make the web server less responsive under load as a process has to finish whatever it was doing before starting handling a new request. Or you can lower max memory setting for PHP, but since you are already consuming all RAM this might not be enough as this is not the core problem (but the number of processes), or something somewhere will break.

Before continuing, did I already say just buy more RAM

Getting to max RAM indicates bad server setup or bad software design.

The first is easy to fix, and at worst it means that you handle more traffic than you were expecting to, and that is a good thing if you make actual money.

The second is just something you are unlikely to fix. You can not spend man years of development in ignoring best practice of limiting PHP memory to the minimal must, and fixing memory problems by raising the limit, and than hope to refactor your code in a month without breaking anything (especially since the site is unlikely to have any automated or even manual testing plan to verify that nothing got broken).

It is cool to ignore software development best practices to reach a “demo” stage in which you can assess if it is worth to put more effort on the project, it is not cool to keep using those page builders and ACF and whatever other RAD junk you used as a base for your final product, or at least if that is the way you prefer to go you need to understand that sooner than later you are going to have to buy more RAM. And in all honesty, as much as I personally dislike page builders, ACF, etc, if your site is not going to grow too much maybe it is the more cost effective solution to buy more RAM when needed.