Reducing Memory Usage in CakePHP 3

Reducing Memory Usage in CakePHP 3

Takahiro Iwasa
(岩佐 孝浩)
Takahiro Iwasa (岩佐 孝浩)
1 min read
CakePHP

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);
Takahiro Iwasa
(岩佐 孝浩)

Takahiro Iwasa (岩佐 孝浩)

Software Developer at iret, Inc.
Architecting and developing cloud native applications mainly with AWS. Japan AWS Top Engineers 2020-2023