Skip to main content

Memory Limits

Learning Focus

By the end of this lesson you will know how to set PHP memory limits correctly based on application behavior, not guesswork.

What memory_limit Does

The PHP memory_limit setting controls the maximum memory a single PHP process can use. If a script exceeds this limit, PHP kills it with a fatal error.

Setting Memory Limits

# Find your php.ini location
/usr/local/lsws/lsphp84/bin/lsphp --ini

# Edit php.ini
sudo nano /usr/local/lsws/lsphp84/etc/php/8.4/litespeed/php.ini
; Set memory limit
memory_limit = 256M

Sizing Guidelines

Application TypeRecommendedRationale
Simple custom PHP64-128 MBLightweight scripts
WordPress (basic)128-256 MBPlugins increase memory usage
WordPress (heavy plugins)256-512 MBPage builders, WooCommerce
Laravel / Symfony128-256 MBFramework overhead

The RAM Formula

Total PHP memory = Workers × memory_limit

Example: 15 workers × 256 MB = 3.84 GB reserved for PHP

Ensure this fits within your server's available RAM alongside OpenLiteSpeed, the OS, and any databases.

warning

Setting memory_limit too high without checking total worker count can cause the server to swap, which is worse than a single PHP script failing. Always calculate the total.

Checking Current Usage

# Check memory usage per lsphp worker
ps aux | grep lsphp | awk '{print $6/1024 " MB PID: " $2}'

# Check total PHP memory usage
ps aux | grep lsphp | awk '{sum += $6} END {print "Total: " sum/1024 " MB"}'

Key Takeaways

  • Set memory_limit based on measured application needs, not maximum values.
  • Always calculate Workers × memory_limit to ensure it fits in available RAM.
  • Monitor actual usage to find the right balance.

What's Next