Symptom
When you start a service on an ECS instance, the system returns a
task: Cannot allocate memory error. The system logs may contain entries similar to the following: 15 10:49:08 iZbp17u xxx systemd: Failed to fork: Cannot allocate memory
15 10:49:08 iZbp17u xxx systemd: frp@8050.service failed to run 'start' task: Cannot allocate memory
15 10:49:08 iZbp17u xxx systemd: Failed to start example systemd custom service unit file 8050.
15 10:49:08 iZbp17u xxx systemd: Unit frp@8050.service entered failed state.
15 10:49:08 iZbp17u xxx systemd: frp@8050.service failed.
15 10:49:08 iZbp17u xxx systemd: Failed to fork: Cannot allocate memory
15 10:49:08 iZbp17u xxx systemd: frp@8049.service failed to run 'start' task: Cannot allocate memory
15 10:49:08 iZbp17u xxx systemd: Failed to start example systemd custom service unit file 8049.
15 10:49:08 iZbp17u xxx systemd: Unit frp@8049.service entered failed state.Cause
This issue occurs because the number of processes on the system exceeds the configured limit.
The Linux kernel uses the
kernel.pid_max parameter to limit the total number of processes. If the total number of running processes exceeds the value of kernel.pid_max, any attempt to create a new process fails and the system reports a task: Cannot allocate memory error.Note If the current number of running processes exceeds two-thirds of the
kernel.pid_max value, increase the value of kernel.pid_max immediately to prevent service disruptions.Solution
Follow these steps to increase the value of
kernel.pid_max. This allows the system to run more processes.Note On 64-bit systems, the maximum value for
kernel.pid_max is 222kernel.pid_max. On 32-bit systems, the maximum value is 32,768.- Connect to the ECS instance.
For more information, see Connect to an ECS instance.
- Run the following commands to check if the current process count exceeds the system limit.
- Check the current number of running processes.
ps -eLf | wc -l - Check the maximum number of processes allowed by the system.
sysctl kernel.pid_max
If the number of running processes is close to or exceeds the maximum limit, proceed to step 3.
- Check the current number of running processes.
- Replace
XXXXwith the new value forkernel.pid_max.- To temporarily set the value, run either of the following commands. The setting is reset after the instance restarts.
sysctl -w kernel.pid_max=XXXXecho XXXX> /proc/sys/kernel/pid_max
- To permanently set the value, run the following commands. The setting persists across instance restarts.
echo "kernel.pid_max=XXXX" >> /etc/sysctl.conf sysctl -p
The new value takes effect immediately. - To temporarily set the value, run either of the following commands. The setting is reset after the instance restarts.