0

My PHP OPcache hits 100% Memory Usage

General info
Zend OPcache    7.0.4-dev
PHP 5.5.23-1+deb.sury.org~trusty+2
Server Software nginx/1.6.2

Here are my Cache Settings:

zend_extension=opcache.so
opcache.memory_consumption=1024
opcache.max_accelerated_files=50000
opcache.revalidate_freq=180
opcache.consistency_checks=0

And some more info about the cache hits:

total memory: 1024.000MB
used memory: 1010.398MB
free memory: 35.086KB
wasted memory: 13.568MB (1.32%)
number of cached files: 10,724
number of hits: 9,576,431
number of misses: 21,450
blacklist misses: 0
number of cached keys: 12,971
max cached keys: 65,407

opcache panel view

Should I be worried about this having 100% Memory usage ? Can someone explain OPcache memory usage in layman terms ?

The Server has 8GB Ram ( Linode )

7
  • Is it better like this ?
    – pyronaur
    Jun 10, 2015 at 19:17
  • 2
    What is the problem you are trying to solve? Or is there a problem? Jun 10, 2015 at 19:30
  • A single question with all the details. Still no good ?
    – pyronaur
    Jun 18, 2015 at 8:21
  • Don't confuse these two scenarios: 1) Something takes 100% of the entire System Memory (RAM, Swap etc.) 2) Something takes 100% of an allocated sub-section of Memory. Scenario 1) is likely to be bad and would indicate either a server upgrade or overhaul of applications/stack. Scenario 2) is your scenario and that's not necessarily bad. It just means: "You have a large cache allocation and it's being used up full to the brim, which is why you get a lot of hits". You don't give any server specs. If you have lots of RAM, you could increase opcache.memory_consumption=2048 for example.
    – JayMcTee
    Jun 18, 2015 at 10:45
  • @AD7six Thanks for the explanation, that's the piece if info I was looking for in the first place, sorry for not being able to explain it right at first.
    – pyronaur
    Jun 18, 2015 at 10:56

2 Answers 2

6
+50

Can someone explain OPcache memory usage in layman terms ?

ldennison plus the various commenters have probably/mostly answered this part.

Another great write-up can be found here: http://www.sitepoint.com/understanding-opcache/

Allow me to summarise:

OPcache allows you to pre-allocate a certain amount of memory. You have set it to 1024, ie. 1GB. This is you telling your system: "cache up to 1GB of compiled php script so the computer doesn't have to turn it form human-readable PHP code to 1's and 0's the computer can understand, each and every time it gets run." Every first time such a script then is run, its pre-compiled version gets added to this cache library. It wasn't in the cache yet on its first run, so this was a cache miss. If it is requested again, and already/still in cache, it gets fetched from cache, saving one 'costly' computing stage. This constitutes a cache hit.

Memory usage just means how much of your 1GB is being used. You see 100% use. That means you have 1GB of php scripts cached and ready to be served from this cache. The cache is full so either no new scripts can be added, or unpopular cache items get pruned, depending on the settings/engine.

Should I be worried about this having 100% Memory usage ?

No.

If your cache hit rate was low, meaning, it was looking in the cache for a file repeatedly but couldn't find it (because it didn't fit in there for example), then you should 'worry'. But your cache hit rate is high, meaning almost every time it wants something from cache, it finds it. That means your 1024 MB cache size, 1/8th of your total server RAM, is very good.

Doubling your cache size will probably not change your cache hit rate significantly. Add 100 MB and it might be enough to go from 96% to 98%.

So the 'problem' you are seeing (100% memory use) doesn't look like a true problem. Yes, it's full, but it isn't missing out on much of value. At this size, it can serve 96% of its needs. That's a great statistic.

Look at it this way...

If a library if full to the rafters, with no more capacity for additional books, but it already carries 96% of all books in the world, is it worth to build an extension? Will it add to the library visitors satisfaction? 96 out of every 100 visitors already find what they want.

2
  • 1
    Done, thanks for the tip. Now both his questions have been answered in one post.
    – JayMcTee
    Jun 18, 2015 at 12:44
  • I ended up doubling the ram dedicated to cache, now I have a 100% cache hit rate and 56% memory usage. Quite happy with the result. Thanks!
    – pyronaur
    Jul 15, 2015 at 7:47
1

Your opcache memory limit (currently set to 1024MB/1GB by the looks of it) should be large enough to cover the size of all the PHP files being cached by Opcache. 1GB for a set of PHP files is large, so that may not be the issue, but you should at least start there. The "File Usage" button on your attached screenshot likely will give you a report of memory usage per cached file.

Doing a bit of quick math on the numbers you posted, 1024MB / 10,724 files ~= 98kb per file average. I suppose that's possible, depending on what you're hosting. Is this a shared server with a lot of different PHP files?

You can set the opcache.max_file_size setting in your cache configuration to exclude any large PHP files from being cached. For example, opcache.max_file_size = 102400 would exclude any PHP files over 100KB from being cached. The value is in bytes for that configuration setting.

Opcache in simple terms takes a PHP file and loads it into memory. Then, when the PHP file is requested at some new point (likely by a different user), it is pulled from memory instead of from disk. That saves both disk I/O time and the time to compile the PHP file, since it is stored in a compiled format (hence the opcode naming). Your currently settings allow for up to 50,000 files to be cached like that, given 1GB memory usage.

2
  • I'm hosting around 15 Wordpress sites at the moment on my own Linode. Each request fires up a lot of includes in Wordpress, so that's why the many files are loaded.
    – pyronaur
    Jun 18, 2015 at 10:54
  • Look in to WordFence Falcon for example to get web-server level full page caching (assuming your pages are quite static). That way a request gets served by the web server before it even has to make calls to the database or get PHP to run any code at all (not even the opcode).
    – JayMcTee
    Jun 18, 2015 at 11:10

Not the answer you're looking for? Browse other questions tagged .