1

Our site is going down over and over again. It's always the same issues. Running out of memory. And the hosts answer is always the same: Buy more RAM.

And that could be needed in the end as well. But I first wanna look trough the codes to track down exactly what's causing the full memory. Our site doesn't have many visitors. So I really doubt there could be the amount of visitors causing a lot of connections and the RAM memory issue. It feels more like there is some script that uses a lot of memories. And we have several cron jobs running every other minute. So would be interesting to see how much memory they use.

So what I would like to see is how much memory all the different scripts/connections/pageloads are using and how many visitors/pageloads there is at the same time when the site is going down.

So, how do I track down php memory usage?

1 Answer 1

0

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.

3
  • Not sure I understand. Well I understand that your point is to buy more RAM. But we DON'T have much traffic. That's why it's feel strange. Why would it be bad to look up what's causing the big RAM memory loss?? Maybe we need to buy more memory anyway but. Can't really see why we just should but without find the cause first. Commented Aug 18, 2018 at 6:13
  • the cause is that you haven't followed best practice software development, and set your php memory limit too high. As you can not easily know in retrospect which part of you code is the problematic one, your only way to discover it is by setting the memory lower, and let PHP fail and generate errors when the limit is passed. This is just not going to be neither easy, nor fun. Commented Aug 18, 2018 at 7:01
  • .... well, maybe it will be easy to find the parts that are problematic, but since you have an already working site, what will you do if you discover than plugin X is the problem? It is not like you can replace it with Y (if something similar enough exists at all) without some manuall configuration and probably coding work Commented Aug 18, 2018 at 7:05

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.