PHP memory management: tracking allocations and reducing peak usage
Profiling a script that hits 512 MB and gets killed. Sharing what I learned about PHP memory tracking.
Measuring allocations:
array_chunk for batch processing:
SplFixedArray uses about 5x less memory than a regular array for integer-indexed data because it avoids the hash table overhead. Worth knowing for large in-memory datasets.
The array_chunk approach is good but loading all IDs into memory first defeats the purpose for very large sets. Better to chunk the IDs at query level: SELECT id FROM table LIMIT 1000 OFFSET n.
gc_collect_cycles() is expensive. Calling it every batch is usually unnecessary. PHP GC runs automatically. Call it only when you have evidence of circular reference accumulation.
hm, but how do you even find WHICH variable is the problem? memory_get_usage just gives a number, not where it went
Xdebug memory profiler or Blackfire shows per-function allocation. For a quick manual approach: add memory_get_usage() logging at key points in the loop and look for which iteration the growth happens.
Doctrine EntityManager identity map is a common source of gradual growth. Call $em->clear() periodically during bulk operations or use $em->detach($entity) after processing each row.
In Eloquent: using cursor() on a query returns a LazyCollection that yields one model at a time. Much better than get() which hydrates all results at once.