Categories
Magento

Opcache wasted memory

Opcache is a vital mechanism for caching opcodes, without which no PHP project can function properly. Opcache provides performance by storing compiled opcodes in RAM. Let’s figure out how it works and what wasted memory is.

When we write our program, we store PHP code in plain text. In the same form it gets to the server, that is, we do not perform any compilation before publishing the code. This means PHP is an interpreted programming language and compilation happens at request time.

When text code from a PHP script is requested for the first time, it is compiled on the fly: converted to byte code and executed on the processor. If opcache is enabled and configured, then after compilation the byte code (opcode) gets into the cache – opcache. The next time, with the next request, compilation no longer occurs and the cached opcode is executed immediately. This provides a huge performance advantage.

For opcache, a dedicated memory area is configured, that is, if there are a lot of scripts in the project, then you need to allocate enough memory to fit them all. Memory consumption can be seen when calling the opcache_get_status function, but it is much better to install a special panel to monitor the status of opcache. Normally, this panel should show something like this:

Wasted memory is marked here, and this value should always be zero whenever possible.

When a PHP script changes, for example, it is edited by another script or new scripts are deployed, PHP checks the file for changes, and if it really has changed, it generates a new entry in opcache for it. At the same time, the memory area that stored the old version of the opcode is not released for the script. New scripts go into the remaining free memory area. To clear this memory, you need to either restart php-fpm or call the opcache_reset function in the php-fpm context (not in the cli context).

It is this memory that is occupied by changed scripts, that is called wasted memory. If the scripts keep changing and the memory is not cleared, it will fill up, the wasted memory will grow, and there will not be enough room for new scripts. In this case, the site will continue to work without opcache, that is, very slowly.

As a result, in order for the site to work quickly, you need to enable opcache, allocate the necessary amount of memory to it and monitor the wasted memory indicator. After each deployment of new scripts, it is better to restart the entire php-fpm.

Sometimes you may notice that you haven’t changed the scripts, but the wasted memory still grows. In this case, check the value of the opcache.huge_code_pages parameter and enable it.

If you have met all the conditions and the wasted memory is still growing, you need to pay attention to your scripts. For example, cache invalidation for specific files may occur there, or even changes may be made to some files on the fly.

Try to find where in the code the opcache_invalidate function is called and do something about it if possible.

As an example, Magento had such a situation. If nothing can be done with the code, then you need to add these files to the exceptions:

at php.ini:

opcache.blacklist_filename="/usr/local/etc/php/opcache-blacklist.txt"

at opcache-blacklist.txt:

; Ignore config files
/var/www/html/app/etc/config.php
/var/www/html/app/etc/env.php

After adding to opcache exceptions, those scripts will not be cached to opcache memory storage, but will be processed by PHP as usual.

If you have any questions or need help, please leave a reply below.

One reply on “Opcache wasted memory”

Leave a Reply

Your email address will not be published. Required fields are marked *