Over the last few months I’ve started enabling PHP realpath_cache_size and realpath_cache_ttl for the performance benefits. At the time I followed the settings I found here. It bugged me that I was blindly setting the size of the cache without actually know what and 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! All these suggestions made blindly without actually knowing how much of the storage is being used. Recently I found the solution to this and thought I’d share it with those who stumble on this post.
Simply create a new php file. You can name it anything, (I used: rpc_size.php) then enter the code example listed here ( http://php.net/manual/en/function.realpath-cache-size.php) in that file. Next, upload it to your web server eg. “/var/www” or “/var/www/html/” then visit yoursite.com/rpc_size.php and it will return how much memory realpath cache is using. Similar to:
So in my case I’m using about 27kb of realpath_cache. I had realpath_cache_size set to 256k and so I was able to reduce it to “realpath_cache_size = 64k” in php.ini.
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! Thus you can see why – to be safe – the PHP dev team made the default settings 16k storage with 120 seconds TTL. :) PHP’s default:
realpath_cache_size = 16k realpath_cache_ttl = 120
My advice set yours to 256K, then check storage using the instructions above. Finally set storage accordingly. Personally I would not set realpath_cache_size higher than 256k unless you have lots of RAM. Even then, 256k is still more than enough. For best performance simply tune realpath_cache_ttl. Basically set large, then tune to down to size.
Here’s my current realpath_cache settings in php.ini for one of my forum sites:
realpath_cache_size = 64k realpath_cache_ttl = 3600
Oh and to view the contents of realpath_cache use:
If safe_mode or open_basedir is used 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.
Enjoy the performance and comfort of “knowing”.