Forgive my ignorance but I’ve not played around with PHP’s garbage collection before. Specifically, I’m running a CakePHP 5.x Command Script and running into memory exhausted issues.
As a test to see how I can clear some memory, I’m trying to run gc_collect_cycles(); on this part of the code:
$connection = ConnectionManager::get('default');
$this->logMessage('Memory usage before query: ' . memory_get_usage(true) / 1024 / 1024 . 'MB');
$studentList = $connection->execute("SELECT DISTINCT
stu.STU_ID AS person_id
FROM SMS.S1STU_DET AS stu
LEFT JOIN Pulse.$studentsTableName AS students
ON students.person_id = stu.STU_ID
LEFT JOIN Pulse.$coursesTableName AS courses
ON courses.person_id = stu.STU_ID
LEFT JOIN Pulse.$unitsTableName AS units
ON units.person_id = stu.STU_ID
LEFT JOIN Pulse.$rawCasesTableName AS cases
ON cases.person_id = stu.STU_ID
WHERE 1 = 1
AND (
students.person_id IS NOT NULL
OR
courses.person_id IS NOT NULL
OR
units.person_id IS NOT NULL
OR
cases.person_id IS NOT NULL
)
")->fetchAll('assoc');
$this->logMessage('Memory usage after query: ' . memory_get_usage(true) / 1024 / 1024 . 'MB');
unset($studentList);
gc_collect_cycles();
$this->logMessage('Memory usage after garbage collection: ' . memory_get_usage(true) / 1024 / 1024 . 'MB');
exit();
And this is the output I get:
2025-02-23 11:32:54 - Memory usage before query: 8MB
2025-02-23 11:32:57 - Memory usage after query: 48MB
2025-02-23 11:32:57 - Memory usage after garbage collection: 44MB
As you can see gc_collect_cycles() didn’t find anything to clean up (it drops to 44MB regardless if I run gc_collect_cycles() or not. So I’m obviously not understanding and/or using this correctly. Is there anyway I can free up memory to get close to the starting 8MB again?