PHP opcache.memory_consumption Explained: Configuration, Performance, and Best Practices
Introduction
The opcache.memory_consumption directive specifies the amount of shared memory allocated to PHP OPcache for storing compiled script bytecode. By caching compiled PHP code, OPcache eliminates the need to parse and compile scripts on every request, significantly improving application performance.
Choosing an appropriate memory allocation helps reduce cache fragmentation and improves cache hit rates, particularly for applications with a large number of PHP files.
This guide explains how opcache.memory_consumption works, how to configure it, recommended values, and common performance considerations.
Test Environment
What Is opcache.memory_consumption?
PHP normally compiles every script before executing it.
Without OPcache:
The same compilation process happens on every request.
With OPcache enabled:
Compiled bytecode is stored in shared memory and reused by future requests.
The opcache.memory_consumption directive determines how much shared memory is available for this cache.
Check the Current Value
Run:
Example:
opcache.memory_consumption => 128 => 128
The value is measured in megabytes (MB).
Locate the Active php.ini File
Example:
Loaded Configuration File: /etc/php.ini
Some Linux distributions store OPcache settings in a separate configuration file, such as:
/etc/php.d/10-opcache.ini
Always modify the active configuration used by PHP-FPM.
Change opcache.memory_consumption
Open the configuration file.
Locate:
opcache.memory_consumption=128
Example:
opcache.memory_consumption=256
Save the file.
Restart PHP-FPM
Apply the changes.
Verify:
Expected output:
Active: active (running)
Verify the Configuration
Run:
Or create a PHP file:
Search for:
opcache.memory_consumption
Recommended Values
The ideal value depends on the number and size of PHP scripts your application loads.
How Much Memory Does OPcache Need?
Every compiled PHP script occupies shared memory.
Applications with:
thousands of PHP files,
multiple frameworks,
many Composer packages,
require more OPcache memory than small websites.
If the cache becomes full, OPcache may evict entries or fail to cache newly compiled scripts, reducing its effectiveness.
opcache.memory_consumption vs memory_limit
These directives serve completely different purposes.
For example:
opcache.memory_consumption = 256
memory_limit = 256M
Although both use memory, they affect different parts of PHP.
Increasing one does not increase the other.
Common Issues
OPcache Memory Is Full
If OPcache runs out of memory, you may observe:
Reduced cache hit rates.
More frequent script recompilation.
Lower overall performance.
Warnings in OPcache status pages or monitoring tools.
Increasing opcache.memory_consumption usually resolves the problem.
Performance Does Not Improve
Possible reasons include:
OPcache is disabled.
The application is very small.
OPcache is frequently invalidated by deployments.
Other bottlenecks, such as database queries or slow I/O, dominate request time.
Configuration Changes Have No Effect
Check that:
PHP-FPM has been restarted.
The correct OPcache configuration file was modified.
OPcache is enabled.
Relationship with Other OPcache Settings
Several OPcache directives work together.
Adjusting opcache.memory_consumption alone may not provide the best performance if other OPcache settings remain poorly configured.
Best Practices
Enable OPcache on production servers.
Allocate sufficient shared memory for your application.
Monitor cache usage and fragmentation.
Restart PHP-FPM after changing OPcache settings.
Review related directives such as opcache.max_accelerated_files and opcache.validate_timestamps.
Conclusion
The opcache.memory_consumption directive determines how much shared memory PHP allocates for storing compiled script bytecode. Adequate memory allocation helps maximize cache efficiency, reduce unnecessary recompilation, and improve overall application performance.
For production environments running modern PHP frameworks or content management systems, tuning this setting alongside other OPcache directives is an important part of server optimization.
Explore More
› PHP opcache.max_accelerated_files Explained: Configuration, Performance, and Best Practices
› PHP opcache.validate_timestamps Explained: Configuration, Code Updates, and Best Practices
› PHP opcache.revalidate_freq Explained: Configuration, Performance, and Best Practices
› PHP opcache.enable Explained: Configuration, Performance, and Best Practices