PHP realpath_cache_size() Function

Beginner
5 min read
Updated: Nov 2026
3 examples
Filesystem

Definition

In PHP, realpath_cache_size() returns how many bytes of memory the realpath cache is using right now.

It is a simple number you can compare with the realpath_cache_size setting in php.ini. If the cache is almost full, PHP may have to look up paths again, so a bigger limit can help large projects.

Syntax

Call realpath_cache_size() with the values you need. Start with this line.

PHP
$bytes = realpath_cache_size();
CallWhat it does
realpath_cache_size()Returns the current memory used by the cache, in bytes.
ini_get('realpath_cache_size')Returns the maximum size allowed by php.ini, such as 4096K.
clearstatcache(true); realpath_cache_size()Clears the cache first, so the size drops back to a very small number.

Do not confuse the function with the php.ini setting of the same name. The function tells you how much is used now. The setting tells you the limit.

Library declarations
PHP
realpath_cache_size(): int

Parameters

ParameterDescription
—This function takes no parameters.

Return Value

Return ValueDescription
intReturns the number of bytes currently used by the realpath cache.

PHP Examples

Example 1 — Show the Cache Size

Resolve a path so the cache has something in it, then print the size.

PHP
<?php
realpath(__DIR__);

echo 'Cache is using ', realpath_cache_size(), ' bytes', PHP_EOL;

How It Works

  1. realpath() resolves a folder and puts the result into the cache.
  2. realpath_cache_size() returns the memory used by all cached entries.
  3. echo prints the number of bytes.
  4. Your own number will be different because it depends on the paths PHP has already used.

Example 2 — See the Cache Shrink After Clearing

clearstatcache(true) empties the realpath cache, so the size should go down.

PHP
<?php
realpath(__DIR__);
realpath(__FILE__);

$before = realpath_cache_size();

clearstatcache(true);

$after = realpath_cache_size();

echo $after < $before
    ? 'Cache got smaller after clearstatcache(true)'
    : 'Cache did not change', PHP_EOL;

How It Works

  1. Two realpath() calls fill the cache and the first size is saved in $before.
  2. clearstatcache(true) removes the cached paths. Without true, the realpath cache would stay.
  3. The size is read again and saved in $after.
  4. Since the cache is now empty or nearly empty, $after is smaller than $before.

Example 3 — Compare Usage with the php.ini Limit

Read the limit from php.ini and show how much of it is used.

PHP
<?php
realpath(__DIR__);

$used  = realpath_cache_size();
$limit = (int) ini_get('realpath_cache_size') * 1024;

$percent = round($used / $limit * 100, 2);

echo "Using $used of $limit bytes ($percent%)", PHP_EOL;

How It Works

  1. ini_get() returns the limit as text, such as 4096K, and (int) keeps the number part.
  2. Multiplying by 1024 turns kilobytes into bytes so both numbers use the same unit.
  3. The division and round() give the percentage of the cache that is in use.
  4. If this number often gets close to 100, raise realpath_cache_size in php. ini.

Frequently Asked Questions

It returns the number of bytes that the realpath cache is using at this moment.
No. The function reports current usage, while the php.ini setting realpath_cache_size is the maximum amount PHP may use.
In current PHP versions the default is 4096K. Older versions used 16K, which was too small for large projects.
Call clearstatcache(true). This clears the realpath cache, so the size drops close to zero.
Only if you have a big project with many files and your cache is often full. Frameworks with thousands of files can benefit from a larger limit.

Next: rename()

rename() renames a file or folder. It can also move it to a different folder if the new pa.

rename() tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful