0Pricing
PHP Academy · Lesson

Memory Management and Performance Tips

Free memory early, avoid large array copies, and use generators for big datasets.

PHP Memory Management

PHP uses reference counting with a cycle collector for garbage collection. Each request starts with a fresh heap — long-running processes (workers, Horizon) can accumulate memory leaks.

memory_limit

Set an appropriate memory limit in php.ini. Exceeding it raises a fatal error.

# php.ini:
memory_limit = 256M

// Check at runtime:
echo memory_get_usage(true)   / 1024 / 1024 . " MB";
echo memory_get_peak_usage(true) / 1024 / 1024 . " MB peak";

All lessons in this course

  1. Profiling PHP with Xdebug
  2. OPcache: Bytecode Caching
  3. Optimizing Database Queries
  4. Memory Management and Performance Tips
← Back to PHP Academy