Reducing Memory Usage in CakePHP 3
When handling a large number of records using CakePHP 3, you may encounter high memory usage issues. CakePHP 3 offers configuration to mitigate this problem.
Result set objects will lazily load rows from the underlying prepared statement. By default results will be buffered in memory allowing you to iterate a result set multiple times, or cache and iterate the results. If you need work with a data set that does not fit into memory you can disable buffering on the query to stream results:
As stated in the official documentation, add the following code to significantly reduce memory usage.
$query->enableBufferResults(false);
// 3.4.0 or earlier
$query->bufferResults(false);