PHP opcache.memory_consumption Explained: Configuration, Performance, and Best Practices

Published: 2026-07-08
Google AdSense 广告位 (待申请通过后开启)

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

Operating System : AlmaLinux 10
PHP Version      : PHP 8.3
Web Server       : Nginx
PHP-FPM          : Enabled
Architecture     : x86_64
Last Tested      : July 2026

What Is opcache.memory_consumption?

PHP normally compiles every script before executing it.

Without OPcache:

Request
    │
    ▼
Read PHP File
    │
    ▼
Parse Source Code
    │
    ▼
Compile Bytecode
    │
    ▼
Execute

The same compilation process happens on every request.

With OPcache enabled:

Request
    │
    ▼
OPcache
    │
 ┌──┴────────┐
 │           │
Hit         Miss
 │           │
 ▼           ▼
Execute   Compile
Bytecode     │
             ▼
        Store in Cache

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:

php -i | grep opcache.memory_consumption

Example:

opcache.memory_consumption => 128 => 128

The value is measured in megabytes (MB).

Locate the Active php.ini File

php --ini

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.

sudo nano /etc/php.d/10-opcache.ini

Locate:

opcache.memory_consumption=128

Example:

opcache.memory_consumption=256

Save the file.

Restart PHP-FPM

Apply the changes.

sudo systemctl restart php-fpm

Verify:

sudo systemctl status php-fpm

Expected output:

Active: active (running)

Verify the Configuration

Run:

php -i | grep opcache.memory_consumption

Or create a PHP file:

<?php
phpinfo();

Search for:

opcache.memory_consumption

Recommended Values

| Application                   | Recommended Value |
| ----------------------------- | ----------------- |
| Small websites                |            128 MB |
| WordPress                     |        128–256 MB |
| Laravel                       |            256 MB |
| Symfony                       |            256 MB |
| Magento                       |            512 MB |
| Large enterprise applications |  512 MB or higher |

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.

| Directive                    | Purpose                                          |
| ---------------------------- | ------------------------------------------------ |
| `opcache.memory_consumption` | Shared memory reserved for compiled PHP bytecode |
| `memory_limit`               | Maximum memory available to each PHP request     |

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.

PHP Request
      │
      ▼
OPcache
      │
      ├── memory_consumption
      ├── max_accelerated_files
      ├── validate_timestamps
      └── revalidate_freq
      │
      ▼
Execute Script

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

Technology Guides →

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

Southeast Asia Insights →