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();
Call
What 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
Parameter
Description
—
This function takes no parameters.
Return Value
Return Value
Description
int
Returns 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;
Output
Cache is using 1160 bytes
How It Works
realpath() resolves a folder and puts the result into the cache.
realpath_cache_size() returns the memory used by all cached entries.
echo prints the number of bytes.
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;
Output
Cache got smaller after clearstatcache(true)
How It Works
Two realpath() calls fill the cache and the first size is saved in $before.
clearstatcache(true) removes the cached paths. Without true, the realpath cache would stay.
The size is read again and saved in $after.
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.