Set PHP realpath_cache_size ‘correctly’

Back in 2012 I started enabling PHP realpath_cache_size and realpath_cache_ttl for the performance benefits. At the time, I followed settings I found here (has since been deleted). It bugged me that I was blindly setting the size of the cache without actually knowing how much storage was being used. In this post I will show you how to view the size and contents of realpath_cache. Thus enabling you to tune settings based on your usage.


Checking realpath_cache_size usage

As you search the web you’ll find blog posts telling you to set realpath_cache_size to 128K, 512K and even 1M. These suggestions are made blindly without actually knowing how much storage is being used. I thought I’d share the solution with those who stumble upon this post.

Simply create a new php file. You can name it anything, (I used: rpc_size.php) then paste the code example listed here, into that file. Next, upload it to your web server (eg. “/var/www/html/”) then visit and that page will return how much memory realpath cache is currently using. Like this:


So in my case I’m using about 178KB of realpath_cache. I had realpath_cache_size set to 4M and so I was able to reduce it to “realpath_cache_size = 1M” in php.ini. The realpath_cache_size default was increased x256 frm 16K to 4M (4096K).

Note that if your using Apache Prefork-mpm mod_php to run PHP (the fastest when loading only PHP and not with static content) then the realpath_cache_size is used for each apache2 client launched. So if maxclients is set to 50 in your Apache2 config and you have say 40 clients spawned, a setting of realpath_cache_size = 1M will apply 40 times! That was 2012 advice, if you are still using Apache, you should avoid mpm-prefork and instead use mpm_event or even mpm_worker with PHP-FPM. PHP’s default:

realpath_cache_size = 4M
realpath_cache_ttl = 120

PHP realpath cache size


Setting realpath_cache_size

My advice, set yours to the new default for PHP 7 of 4M, check storage using the instructions above and finally reduce the size only if you have low server memory. For best performance also tune realpath_cache_ttl. Again set large, then tune to down to size.

Here’s my current realpath_cache settings in php.ini on a 3GB StackLinux VPS hosting this blog:

realpath_cache_size = 1M
realpath_cache_ttl = 300

Oh and to view the contents of realpath_cache use:


If safe_mode or open_basedir are enabled in your php.ini then see this bug (workaround at the bottom of the page).Basically realpath cache is not used if safe_mode is on or if open_basedir restriction is enabled… thus causing lots of calls to lstat.



Originally posted: Sep 6th, 2012 | Last updated: July 21st 2021

Tags: , ,