Help Center/ Elastic Cloud Server/ Troubleshooting/ Linux ECS Issues/ What Do I Do If Error "Cannot allocate memory" Is Displayed?
Updated on 2023-09-27 GMT+08:00

What Do I Do If Error "Cannot allocate memory" Is Displayed?

Symptom

A Linux ECS has sufficient memory, but the error "Cannot allocate memory" is displayed during command execution or service start.

Command output:

root@localhost:~# free -m
          total       used       free     shared    buffers/cached    available
Mem:      3890         125       3179          2          504          3463
Swap:        0          0         0
root@localhost:~# uname -a
-bash: fork: Cannot allocate memory

Possible Causes

The number of running processes or threads in the system reaches the upper limit. The upper limit is specified by the kernel parameter /proc/sys/kernel/pid_max.

Solution

  1. Remotely log in to the ECS.
  2. Run the following command to check the number of running processes:

    ps -eLf | wc -l

    The command output is as follows:

    32753

    You can also run the sar –q command to view the number of running processes in the plist-sz column.

    If "command not found" is displayed, run the command to install the software package first.

    • CentOS: yum install sysstat
    • Ubuntu: apt install sysstat

    sar -q

    The command output is as follows:

    ...
    00:00:01      runq-sz  plist-sz   ldavg-1   ldavg-5  ldavg-15  blocked
    10:45:01        0     32722      0.10      0.12      0.16     0
    10:46:01        1     32730      0.21      0.15      0.17     0
    10:47:01        2     32752      0.07      0.11      0.15     0
  3. Run the following command to check the maximum number of processes configured in the system:

    cat /proc/sys/kernel/pid_max

    32768

    If the number of running processes is close to or has reached the maximum number of processes, you need to increase the value of /proc/sys/kernel/pid_max.

  4. Run the following commands to change the value of /proc/sys/kernel/pid_max, for example, to 65530.

    echo "kernel.pid_max=65530" >> /etc/sysctl.conf

    sysctl -p

    The command output is as follows:

    vm.swappiness = 0
    net.core.somaxconn = 1024
    net.ipv4.tcp_max_tw_buckets = 5000
    net.ipv4.tcp_max_syn_backlog = 1024
    kernel.pid_max = 65530