An alarm triggered by a Lararel queue is reported by a server. the memory usage is too high. it is strange that other servers in the cluster are okay. However, from past experiences, there is a strange answer behind every incredible question.
First, check the memory usage through "free-m" and find that 6893 M is used, with 976 M left:
Free
Then, click "top" to check which processes occupy a large amount of memory and sort by memory using "shift + m:
Top
Although we can confirm that the available memory of the system is insufficient through the free command, but through the top command we did not find any process with large memory usage, where is the memory?
We mentioned at the beginning that there is a problem with only one server in the cluster and other servers are normal, so we compared the process list of the problematic server and the normal server, several processes are added to the problematic server:
/Usr/local/bin/php artisan queue: listen
/Usr/local/bin/php artisan queue: work
After confirmation, they are Laravel queues. Although intuition tells us that the problem is associated with it, the process itself does not occupy much memory. in the case that the cause cannot be confirmed immediately, we use the exclusion method to switch the queue to another normal server to see if the problem will be reproduced. after a while, the same problem occurs again.
Since commands such as free and top cannot determine the memory destination, let's look at "meminfo 」:
Meminfo
As shown in, a large amount of memory is consumed by Slab. Further, it is consumed by SReclaimable. that is to say, the memory is consumed by some reusable Slab. For more information, see slabtop:
Slabtop
A large amount of memory is consumed by dentry. if you are the same as me, you cannot figure out what it means. search, use Google to flip the wall, and use AOL instead of Baidu, I found the following introduction:
-
Troubleshooting of insufficient system memory caused by excessive memory usage by Linux server Cache
-
Troubleshooting of insufficient system memory caused by excessive memory usage by Linux server Cache (continued)
In short, the memory dentry caches the information of recently accessed files. if a large number of files are frequently operated, dentry will continue to increase, so the problem becomes to check whether the Laravel queue has similar problems.
As mentioned above, the Laravel queue has a listen process and a work process. we can determine from the name that the former is the main process and the latter is a sub-process, the sub-process is a working process. However, when I directly trace the sub-process through strace, I am prompted that the sub-process does not exist. further debugging found that the sub-process will be restarted constantly!
Since it is difficult for us to directly track sub-processes, we can start with the parent process to track the file operations of sub-processes:
shell> strace -f -e trace=open,close,stat,unlink -p $( ps aux | grep "[q]ueue:listen" | awk '{print $2}')
It is a pity that Laravel itself is known as a master framework and relies on a lump of files. Therefore, the tracking results are filled with a large number of normal open, close, and stat operations of framework files, try to track only unlik operations:
shell> strace -f -e trace=unlink -p $( ps aux | grep "[q]ueue:listen" | awk '{print $2}')
The Laravel queue is frequently used to delete files. each time the sub-process is restarted, the deletion is performed:
Unlink ("/tmp/. ZendSem. aXaa3Z ")
Unlink ("/tmp/. ZendSem. teqiniy ")
Unlink ("/tmp/. ZendSem. Bn3ien ")
Unlink ("/tmp/. ZendSem. V4s8RX ")
Unlink ("/tmp/. ZendSem. PnNuTN ")
Because the names of temporary files are different, a large amount of dentry cache is consumed. Check the Laravel queue documentation and find that the Laravel queue actually provides a daemon mode without restarting, so that a large number of temporary files will not be created frequently, and a large amount of dentry cache will not be consumed, recommended.
If it is unavoidable to frequently create a large number of temporary files, we can delete recoverable slab (including dentries and inodes) by setting drop_caches to 2 as described in the Linux document ), relatively crude:
shell> echo 2 > /proc/sys/vm/drop_caches
In addition, you can set vfs_cache_pressure to be greater than 100 to increase the recovery probability, which is more gentle:
shell> echo 1000 > /proc/sys/vm/vfs_cache_pressure
According to the test results, the vfs_cache_pressure function is limited. of course, it may be because my posture is incorrect. There are also some materials saying that min_free_kbytes can also solve such problems, but in view of the danger of this parameter, I suggest you do not modify it. I will not talk about it here. if you are interested, you can check the relevant information on your own.