Every application consumes server resources. At times, buggy applications create resource crunch on the server and result in memory errors.
One such error is “mmap failed cannot allocate memory“.
This error primarily happens due to memory shortage. Unfortunately, finding the real reason for high memory usage can be tedious.
At Bobcares, we often get requests to solve server memory issues as part of our Server Management Services.
Today, we’ll see how our Support Engineers analyze and fix mmap memory issues in the servers.
When do we see “mmap failed cannot allocate memory” error?
Firstly, we’ll have a look at the typical scenario where this error pops up.
Simply stated, mmap is a system call that maps files or devices into server memory. This mmap() function creates a new mapping upon every file request. And, when the memory in the server is running out, the logs will show memory errors as :
mmap() failed: [12] Cannot allocate memory
Basically, this error happens due to insufficient memory allocation on the server.
From our experience in managing servers, we see this error in applications that are memory intensive. Some typical examples include complex Magento installation, or installing packages via PHP composer in containers with low memory allocation.
How we fix mmap error?
Unfortunately, the mmap error is very broad. The real challenge is to find the cause of high memory usage. And, the solution depends on the underlying reason.
Now, we’ll see how our Dedicated Engineers troubleshoot and fix the mmap error.
Recently, a customer approached us to fix his application that often gets stuck. The website was showing a blank page in the browser too. Additionally, the customer was getting high memory usage alert emails as :
cmd:/opt/cpanel/ea-php72/root/usr/bin/php-cgi
User:abc PID:4971 PPID:25467 Run Time:1(secs) Memory:212248(kb)
Fixing the error involves a series of steps. They are:
1.Understanding server resource usage
Here, our Support Engineers began troubleshooting by checking the server resources. This includes checking the server load, server logs, finding the top resource consuming processes, etc.
On checking the web server logs, we could see the error :
mmap() failed: [12] Cannot allocate memory
Seeing the memory error, we further checked the memory usage on the server. As expected, the results showed that the server had almost zero unused memory. The exact results were:
root@server [/usr/local/apache/logs]# free -m
total used free shared buff/cache available
Mem: 3950 3684 86 46 180 17
Swap: 0 0 0
2. Identifying the high memory process
OK. We knew that the server was having memory crunch. But, What was causing it? That was the next question.
To find the real user consuming most memory, we looked on the results of top command. It showed up as :
PID SYSCPU USRCPU VGROW RGROW RDDSK WRDSK RUID EUID ST EXC THR S CPUNR CPU CMD
4220 0.95s 1.40s 0K 304K 88K 68K mysql mysql -- - 27 S 1 24% mysqld
11282 0.24s 1.34s 0K 0K - - abc - NE 0 0 E - 16%
11236 0.26s 0.92s 64288K 64224K 2196K 0K abc -- -1 R 0 12% php-cgi
11373 0.20s 0.94s 276.4M 100.2M 0K 0K abc N - - 1 R 0 11% php-cgi
Here, the top resource consuming processes were MySQL and the PHP process of the user “abc“.
Subsequently, we checked the MySQL processes to find the high resource process. And, we found that there were too many mysql queries from the WordPress under ‘abc’ user.
root@server# mysqladmin proc stat
+-------+------------------+-----------+----------------+---------+------+-------+------------------+----------+
| Id | User | Host | db | Command | Time |
State | Info | Progress |
+-------+------------------+-----------+----------------+---------+------+-------+------------------+----------+
| 28177 | abc_multiadmi | localhost | abc_multiwp | Sleep | 0 |
| | 0.000 |
| 28179 | abc_multiadmi | localhost | abc_multiwp | Sleep | 0 |
With that, we summarized that the problem was with the WordPress of the user.
3. Fixing mmap error
Moving on, to fix the “mmap failed cannot allocate memory” error, we had two options. We’ll now check the details.
a)Tweaking server resources
Of course, the first and may be the easiest fix is to upgrade the server resources. Here, our Support Engineers suggest customer to add in additional memory to the server. For customers running their servers in cloud platform, adding memory can be done in a jiffy, without causing website downtime.
However, adding more memory involves additional charges which may not be fine for some server owners.
b)Optimizing buggy scripts
Similarly, another method is to fix the buggy scripts that uses high memory. The scripts can cause this error due to bad programming, faulty plugin, etc.
In the above case, our Support Engineers found that the WordPress was using a faulty plugin. We disabled the plugin and the server resource usage was back to normal again.
Again, if you have a custom script written by a developer, you may need to optimize them for memory usage.
[Seeing mmap memory error in the server ? Our Server Engineers can fix it for you.]
Conclusion
In short, “mmap failed cannot allocate memory” error happens due to memory issues on the server. Today, we saw how our Dedicated Engineers helped a customer to fix the mmap error by identifying and correcting the faulty application.
I ran into this issue today. It boiled down to the primary file system being at 100% utilization. Once drive space was cleared up, it was able to swap again and the errors went away.
helpful