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 6, 2012 | Last updated: April 22nd 2019

Tags: , ,

Set PHP realpath_cache_size ‘correctly’

13 Responses

  1. excellent post. i like yourself was setting this blindly. glad to find a way to correctly find out what realpath cache should be set to. big ups!

    Anonymous User November 29, 2012 at 9:22 am #
  2. thanks. Esp for heavier web apps such as Magento, Drupal, etc this can really help performance.

    @hydnj January 5, 2013 at 3:50 am #
  3. Thanks to this tutorial I can see what is going on behind the php engine.
    But I am puzzled by the output of int(x) value. The value goes up after a couple of refreshes then drops to 0 or a low value and starts rising again.

    My interpretation is that after hitting the value of real_path_cache_size the cache resets it selves and start over again.
    So I tried to raise the real_path_cache_size to even 128M but I think there is a limit around 256K.
    I don’t get higher int(x) values.

    Is there a limit somewhere?

    Bottom line after disabling open_basedir restriction the real_path_cache started working, And all though the cache seems to be resetting itself after seconds I noticed a 10-15% speed gain.

    Karel February 24, 2013 at 1:14 pm #
  4. Remember to set realpath_cache_ttl. I don’t know the limit but 128M is crazy to try. I’ve seen it beyond 256k with large Magento stores. I would not set to more than 1024k, better to reduce ttl in such cases as it works best on busy websites where there’s 100’s of page hits per minute or more, then a setting of even realpath_cache_size = 512k realpath_cache_ttl = 60 can improve throughput.

    @hydnj February 24, 2013 at 1:21 pm #
  5. Thank you for great post. But I’m receiving negative int(-x) values. I checked my php.ini for open_basedir, but is starting with ; and there was no value assign after “open_basedir =”, and my safe mode is off. And realpath cache = 32k is set in php.ini. This a magento store.

    Mike April 6, 2014 at 12:08 pm #
  6. It even goes to int(-4xxxxx)

    Mike April 6, 2014 at 12:12 pm #
    • Could be this bug:

      So does not affect funtion, just make sure to gather what size you need within the time of realpath_cache_ttl you’ve set.

      Busy site then use shorter TTL.

      Hayden James January 5, 2015 at 6:09 am #
  7. Great writeup.

    Just a couple quick questions. I am running a magento site roughly 15,000 items, and the default settings were 32kb cache, 7600 ttl, and the site was runnning slllllooooow. I adjusted the settings to cache: 256kb / ttl: 300 and noticed an immediate speed bump. Since doing the update i have been browsing around and checking the realpath_cache_size(); value and it bounces around between 140-200kb so i am guessing that the 256kb is a good value to stick with for now. But I have no clue what to do with the ttl value. I picked 300 because the average session duration is under 5min and figured this would cover that, but should it be even lower? Do you recommend just dropping it by 100 each week then reviewing my logs to see average load times?

    thanks again for great write up!

    Mickey September 4, 2014 at 9:11 pm #
    • Set your TTL value based on traffic. If say you get avg of 1 visit per min then (it may not be useful) set to 3600 seconds. If you have 100 visits per min then leave default of 120 seconds or for 1000 hits per min set to 60 seconds. (These are just examples)

      More traffic makes this more useful feature to enable.

      Hayden James February 23, 2015 at 4:30 pm #
  8. Thank you for this!

    Jan Reilink October 23, 2014 at 8:22 am #
  9. Post has been updated.

    Hayden James September 6, 2016 at 6:46 pm #
  1. Optimize PHP performance, check and set PHP realpath_cache_size - Sysadmins of the North - October 23, 2014

    […] = window.adsbygoogle || []).push({});   All kudos for this goes to Hayden James, who posted on this back in november […]

  2. Stillat | PHP Slow Response Times - February 1, 2015

    […] For more information on this setting, and how to tune it consider reading Hayden James’s article Set PHP realpath_cache_size “correctly”. […]

Leave a Reply