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 KAKEHASHI Inc.
Involved in the requirements definition, design, and development of cloud-native applications using AWS. Now, building a new prescription data collection platform at KAKEHASHI Inc. Japan AWS Top Engineers 2020-2023.