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
- Profiling PHP with Xdebug
- OPcache: Bytecode Caching
- Optimizing Database Queries
- Memory Management and Performance Tips