|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
[2019-02-01 09:05 UTC] nikic@php.net
-Status: Open
+Status: Duplicate
[2019-02-01 09:05 UTC] nikic@php.net
|
|||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Sat Nov 01 00:00:01 2025 UTC |
Description: ------------ Problem happens if I request big table or table with lots of data in rows. Memory usage increasing drastically while fetching one row at a time. If I query only one field, ex id, memory usage grows slower. The code in example gives "PHP Fatal error: Allowed memory size" after fetching ~175 rows. Test example generates table, fill it and tries to fetch data row by row. Tested the code in php 7.2 and it worked as expected. Test script: --------------- <?php $db_name = ''; $db_user = ''; $db_pass = ''; ini_set('memory_limit', '10M'); $cn = 0; $pdo = new \PDO('mysql:host=localhost;dbname=' . $db_name, $db_user, $db_pass); $pdo->setAttribute(\PDO::MYSQL_ATTR_USE_BUFFERED_QUERY, false); $pdo->exec('DROP TABLE IF EXISTS test_mem_leak'); $pdo->exec('CREATE TABLE IF NOT EXISTS test_mem_leak (id INT AUTO_INCREMENT, txt TEXT, PRIMARY KEY(id))'); $fill = str_repeat('test-', 10000); for ($i = 0; $i < 500; ++$i) { $pdo->exec("INSERT INTO test_mem_leak SET txt = '$fill'"); } $q = $pdo->query('select * from test_mem_leak'); while ($d = $q->fetch()) { echo $cn . ' ' . round(memory_get_usage() / (1024 * 1024), 3) . "MB \n"; ++$cn; } echo "ok: $cn\n"; Actual result: -------------- PHP Fatal error: Allowed memory size of 10485760 bytes exhausted (tried to allocate 53248 bytes) in /home/me/code/tmp/1.php on line 16