If your PHP or other process on a 1GB LAMP VPS throws “fork: Cannot allocate memory,� the immediate cause is almost always exhausted memory or a hit process/thread limit, not a broken script. You can fix it by freeing RAM, raising the process limit, or adding swap—often within minutes.
What “Cannot allocate memory� Actually Means
The fork() system call creates a new process by duplicating the calling process. The kernel must allocate a small amount of memory for the new process’s task struct and stack. When it fails, the kernel returns ENOMEM—which PHP surfaces as “Cannot allocate memory.� The two most common triggers are:
- Physical memory exhaustion: The system has no free RAM and no swap left, so even the tiny allocation for a new process fails.
- Process/thread limit reached: The cgroup or system-wide
pids.maxorRLIMIT_NPROChas been hit, often by many PHP-FPM workers or MySQL threads.
A less common cause is vm.max_map_count exhaustion, but on a 1GB VPS the first two dominate.
Check Memory and Process Limits First
Before changing anything, confirm what is actually exhausted. Run these commands as root:
free -m
Look at the “available� column—if it is near zero and swap is 0, memory is the problem.
cat /sys/fs/cgroup/pids.max
If this shows a number like 512 or 1024, and you have hundreds of PHP-FPM workers, you may be hitting the process limit. Also check ulimit -u for the user running PHP-FPM.
To see how many processes exist right now:
ps -eLf | wc -l
Free Up Memory Quickly
If memory is the issue, the fastest fix is to stop or restart memory-hungry services. On a typical LAMP stack, MySQL/MariaDB and PHP-FPM are the biggest consumers.
- Restart MySQL:
systemctl restart mysql(ormariadb). This releases query caches and buffers. - Restart PHP-FPM:
systemctl restart php8.1-fpm(adjust version). Each worker can use 30–80MB, so reduce the number of workers inpool.d/www.conf. - Check Apache: if you use prefork MPM, each connection spawns a process. Switch to
eventMPM or lowerMaxRequestWorkers. - Kill runaway processes:
ps aux --sort=-%mem | headto spot a single process eating 500MB.
After freeing memory, the error should disappear immediately. If it returns, you need a structural fix.
Add Swap Space (The Most Reliable Fix)
On a 1GB VPS, swap is often disabled. Adding a 1–2GB swap file gives the kernel breathing room and prevents the “Cannot allocate memory� error under normal load. On most modern VPSes (using systemd and a standard kernel):
fallocate -l 2G /swapfile
chmod 600 /swapfile
mkswap /swapfile
swapon /swapfile
echo '/swapfile none swap sw 0 0' >> /etc/fstab
If fallocate fails (some filesystems don’t support it), use dd if=/dev/zero of=/swapfile bs=1M count=2048. After adding swap, set vm.swappiness=10 in /etc/sysctl.conf to avoid excessive swapping.
Swap is not a cure-all—if your workload genuinely needs more than 1GB RAM, swap will cause heavy disk I/O and slow the server. But for typical LAMP sites, it eliminates the fork error.
Raise the Process Limit (If That’s the Cause)
If pids.max is the limiting factor, you can increase it. On a systemd-based VPS, edit /etc/systemd/system.conf and set DefaultTasksMax=2048 (or higher), then reboot or restart the service. For cgroup v2, you can also write directly:
echo 2048 > /sys/fs/cgroup/pids.max
But this is temporary—it resets on reboot. The persistent fix is to reduce the number of PHP-FPM workers. In /etc/php/8.1/fpm/pool.d/www.conf, set pm.max_children to a value that fits your memory. A safe formula for a 1GB VPS:
- Reserve ~200MB for the OS and MySQL.
- Assume each PHP-FPM worker uses ~40MB (can be higher with WordPress).
- Set
pm.max_childrento 10–15.
If you have many concurrent visitors, you may need to tune MySQL’s innodb_buffer_pool_size down to 128M or less.
Common Mistakes and Misconceptions
Myth: “It’s a disk space problem.� No—this error is about memory, not storage. df -h won’t help.
Mistake: Increasing PHP memory_limit in php.ini. That setting only affects a single PHP script’s memory ceiling, not the kernel’s ability to fork. It won’t fix this error.
Mistake: Ignoring OOM killer logs. Check dmesg | grep -i oom or journalctl -k—if the OOM killer is active, it’s killing processes to free memory, which can leave the system in a state where fork fails intermittently.
Misconception: “I need to upgrade to 2GB RAM.� Not always. Many 1GB VPSes run fine with swap and tuned services. Upgrade only if you consistently use more than 1GB of active memory (not cache).
When to Consider Upgrading RAM
If you’ve added swap, reduced PHP-FPM workers, and still see the error during normal traffic, your workload genuinely exceeds 1GB. Signs you need more RAM:
- Swap usage is constantly above 500MB.
- MySQL performance degrades because the buffer pool is too small.
- You run multiple heavy applications (e.g., Node.js, Elasticsearch, or a large WordPress site with many plugins).
In that case, upgrading to 2GB is a legitimate fix. But first, try the software tweaks—they’re free and often sufficient.
Frequently Asked Questions
Why does this happen even when free -m shows free memory?
Because free shows “available� memory, which includes reclaimable cache. The kernel can usually reclaim cache, but if it’s under heavy pressure or the allocation is for a huge process (like a PHP script with a 512M memory_limit), it may still fail. Also, the process limit can cause the error even with free RAM.
Can I fix this by editing php.ini memory_limit?
No. memory_limit controls how much memory a single PHP script can use, but the fork error is about the kernel allocating a new process. Lowering it might reduce memory usage per worker, but it won’t directly fix the fork failure.
Is swap on a VPS bad for performance?
Swap is slower than RAM, but it’s a safety net. If your server rarely uses swap, it’s fine. If it’s constantly swapping, you need either more RAM or to reduce memory usage. Use vm.swappiness=10 to keep swap usage minimal.
How do I know if I hit the process limit?
Run cat /sys/fs/cgroup/pids.current and compare it to pids.max. If they’re close, that’s the cause. Also check ulimit -u for the PHP-FPM user. If you have 200+ processes, that’s a red flag.
Will restarting Apache/MySQL fix it permanently?
No, it only frees memory temporarily. The error will return when memory fills up again. You need to address the root cause: add swap, reduce workers, or upgrade RAM.
Related
Method: the standard formula for this calculation, rounded as each result states. See how the calculators work or report an error.
More cards in this drawer
- Poco X3 GT Dimensity 1100 67W Fast Charging Launched?5.00 min/km
- Flash Converter: What It Is & How to Use It
- Choosing a Pipe Flow Meter for Swirling Flow
- Motorola Phone Recovery: How to Recover Data
- Convert 2D Videos to Oculus Rift VR: How-To
- DRAM Light on Motherboard: Meaning & Fixes5.00 min/km