The memory issue is caused by fetchAll(), which loads the whole result set into memory.
Fetch and process one row at a time instead, so PHP does not need to hold the entire table in an array.
<?php
$stmt = $pdo->prepare('SELECT * FROM largeTable');
$stmt->execute();