PHP 7 is not messing around when it comes to performance. On this WordPress blog you are reading right now, just switching to PHP 7 reduced memory usage in the Media Uploader by 78%!
How I Came to Know This…
As you may know, I’m running this WordPress multisite install on a $0.99 VM from Atlantic.net. I’ve been super happy with this server (which I’m lucky to have since the deal is no longer available) and have really enjoyed figuring out how to make everything work with only 256M of RAM (see my previous post for more on how I’ve done this). This week, I thought this grand experiment was going to come to an end as I ran into error after error trying to upload multiple images though the WordPress Media Uploader.
The WordPress uploader is a known to use quite a bit of memory when using the php-gd library to resize uploaded images. With my puny 256M, it is no wonder I was running into this. I installed the free New Relic server monitoring to get some pretty graphs of what was going on and, sure enough, PHP 5.5 was going bonkers when I tried to upload multiple images.
If you look at the Physical memory graph, you can see where this goes bad. Once the red swap space starts growing, it is an indication the machine has filled up the RAM and has started using the disk in a desperate attempt to keep things running. This essentially locks up the server as the disk is nowhere near fast enough for this job. This is what the user experience was in the browser as well – 1 or 2 images would upload, and the rest would fail with a generic “HTTP Error” message in red after doing nothing for many minutes.
New Relic is pretty great in that you can see exactly what server process has used all your memory, and it confirms what we suspected: PHP is the culprit.
I did a lot of optimizing to try to get this memory usage down, including changing the WordPress memory limit, the PHP memory limit, and fiddling with the caching settings. Nothing made a difference.
I started looking for other things I could try and seriously considered just upgrading to the $4.97 per month VM with 512M of RAM. But I came across this article about Symphony framework and how much less memory certain code used on PHP 7 compared to PHP 5.6 and thought the upgrade was worth a shot.
It was a little bit messy getting PHP 7 on Ubuntu and I longed for CentOS 7 where I could use the excellent Remi repo, but using a Digital Ocean tutorial, I was able to get PHP upgraded and was ready to run another test.
I tried uploading 4 images to start. The results were so unbelievable, I did another test with many many more images to make sure I hadn’t done my test wrong. The Media Uploader on PHP 7 processed as many images as I could throw at it and the memory usage barely budged. With PHP 5.5, the CPU spiked, the load went up, and a php5-fpm process sat using 80%+ of the RAM for minutes and minutes. With PHP 7, the php7.0-fpm process never went past 15% memory usage and the CPU barely jumped at all. Check out the telemetry from New Relic:
You can see the CPU spike as PHP 7 processes the images, but there’s hardly a bump in the RAM usage at all.
According to New Relic, PHP 7 used a maximum of 41 MB to process the dozen images I threw at it. With PHP 5.5, memory usage topped out at 190 MB for the same job. That’s an incredible improvement and it allows me to keep enjoying my $0.99 VM!
I knew PHP 7 was good, but I had no idea I’d see gains this dramatic. If you run a WordPress server, go install PHP 7 right now – I’m a believer.