I have a problem with simple update in my console command (Symfony based).
This is my code:
private
function showMemoryUsage()
{
$memory = memory_get_usage();
$memory/= 1024;
if ($memory < 1024) {
$this->output->writeln('Memory usage: ' . round($memory, 2) . ' KB');
return;
}
$memory/= 1024;
if ($memory < 1024) {
$this->output->writeln('Memory usage: ' . round($memory, 2) . ' MB');
return;
}
$memory/= 1024;
if ($memory < 1024) {
$this->output->writeln('Memory usage: ' . round($memory, 2) . ' GB');
return;
}
}
// ...
$stmt = $conn->prepare("UPDATE product_counters SET counter = :count WHERE id = :id");
$this->showMemoryUsage();
foreach($cursor as $k => $counterData) {
if ($counterData['_id']) {
$stmt->execute([':count' => (int)$counterData['count'], ':id' => (int)$counterData['_id']]);
$stmt->closeCursor();
}
}
$this->showMemoryUsage();
I have to update 88 000 rows.
First memory usage is only 14 MB. Second memory usage is about 400 MB. How can I decrease this usage? I've tried to use gc_enable
&& gc_collect_cycles
, I've tried to unset $stmt
after foreach, but with no result. Can you suggest a way to decrease memory usage?