php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #51648 Memory leak in infinite loop
Submitted: 2010-04-23 16:01 UTC Modified: 2010-09-20 14:47 UTC
Votes:1
Avg. Score:4.0 ± 0.0
Reproduced:1 of 1 (100.0%)
Same Version:1 (100.0%)
Same OS:1 (100.0%)
From: shooreek at gmail dot com Assigned:
Status: Not a bug Package: SQLite related
PHP Version: 5.3.2 OS: WinXP SP3
Private report: No CVE-ID: None
 [2010-04-23 16:01 UTC] shooreek at gmail dot com
Description:
------------
Run PHP script with infinite loop from command line. Call each loop database function from extension (I tested SQLite and Mysqli). Open task manager. You will see that memory use is growing up, while PHP function memory_get_usege() reports that memory usage is constant. The memory usage may grow up to 1,5Gb or even more (depends on script code size), even if memory_limit is set to 8Mb.

How is this possible?!

Test script:
---------------
<?php

$dbfile = 'mysqlitedb.db';
if(file_exists($dbfile)) unlink($dbfile);
$db = new SQLite3($dbfile);

$db->exec('CREATE TABLE foo (id INTEGER, bar varchar(30))');

$db->exec("INSERT INTO foo (id, bar) VALUES (1, 'This is a test')");

$i=0;
while(1)
{
	$i++;
	$db->exec("INSERT INTO foo (id, bar) VALUES ($i, 'This is a test')");
	$db->exec("UPDATE foo SET bar='This is memory leak' WHERE id=$i");

	echo "$i ".memory_get_usage()." ".memory_get_usage(true)."\r";
}

?>


Expected result:
----------------
Constant memory usage.

Actual result:
--------------
Memory usage grown up (script output):

iteration, memory_get_usage(), memory_get_usage(true), memory usage(from taskmgr), virtual memory usage(from taskmgr)

2131 330384 524288 - 1068Kb 5356Kb
4661 330384 524288 - 1148Kb 5440Kb
10974 330384 524288 - 1336Kb 5652Kb
24985 330384 524288 - 1824Kb 6140Kb
43894 330384 524288 - 2516Kb 6828Kb

As you can see, memory usage is not constant. Why?!

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2010-04-24 18:43 UTC] felipe@php.net
I can't reproduce it on Linux.
 [2010-05-02 16:24 UTC] cbandy at jbandy dot com
Inserting an infinite number of rows could easily lead to mem usage within SQLite (not PHP).

By executing only UPDATE in the loop, DB size should stay constant. Then results should show PHP-SQLite execution leaks.
 [2010-05-04 11:48 UTC] shooreek at gmail dot com
cbandy, you are right: with UPDATE or SELECT queries mem usage remains constant. But with INSERT queries mem usage grows up. I discovered some way to free this memory: you should unlink SQLite DB file and create new one, because $db->close() is _not_ enough! May be there are another ways to do it, but I do not know about their existence. May be someone knows?

I think it real bug and it should be fixed. So, it is SQLite extension related bug.

There is no such problem in Mysqli.

P.S. I first post I wrote that Mysqli have the same problem as SQLite. My fault. The problem was in multiple calls mysqli_init() function. With SELECT, UPDATE, INSERT or UPDATE commands Mysqli do not have memory leaks. If memory leaks related with multiple mysqli_init() calls is a bug, I can report it in separate. Just let me know.

Thank you for your attention.
 [2010-05-07 13:19 UTC] shooreek at gmail dot com
-Package: Scripting Engine problem +Package: SQLite related
 [2010-05-07 13:19 UTC] shooreek at gmail dot com
Pakage changed to SQLite related
 [2010-05-07 16:51 UTC] crrodriguez at opensuse dot org
Cannot reproduce in linux, tell us if you have 3rd party extensions installed as 
well if you have modified php.ini.

maybe Pierre can check this one out..
 [2010-05-08 11:22 UTC] shooreek at gmail dot com
Now I cannot provide exact information about modification of php.ini, but I can provide it later (two week's holiday). 

I modified memory_limit, post_max_size, extension_dir, uncommented some extensions like misql, mysqli, pdo_sqlite, sqlite3, curl. Nothing serious, only extensions from binary VC6 x86 Thread Safe (2010-Mar-04 20:11:08).

As you can see, memory leak rate is not so great. You cannot reproduce leak at all, can you?
 [2010-05-25 15:29 UTC] shooreek at gmail dot com
php.ini modifications: 
-; extension_dir = "ext"
+ extension_dir = "ext"
-;extension=php_sqlite3.dll
+extension=php_sqlite3.dll


My new test shows that "leak" behaves like caching. It seems to me that SQLite cache entire DB in memory until some limit.

Here are the results. First row - date ad time, second - php.exe memory usage (from taskmgr), third - size of DB file. And so on. About 100,000 loops on virtual machine (real machine is to slow with hard disk). As you can see, cache limit, if any, is about 2Mb.



2010.05.25 16.13.32
php.exe      1 116 K
75 776 mysqlitedb.db
---------------
2010.05.25 16.16.31
php.exe      1 512 K
423 936 mysqlitedb.db
---------------
2010.05.25 16.19.31
php.exe      1 848 K
711 680 mysqlitedb.db
---------------
2010.05.25 16.22.30
php.exe      2 124 K
961 536 mysqlitedb.db
---------------
2010.05.25 16.25.30
php.exe      2 380 K
1 184 768 mysqlitedb.db
---------------
2010.05.25 16.28.29
php.exe      2 620 K
1 389 568 mysqlitedb.db
---------------
2010.05.25 16.31.29
php.exe      2 828 K
1 575 936 mysqlitedb.db
---------------
2010.05.25 16.34.29
php.exe      3 028 K
1 753 088 mysqlitedb.db
---------------
2010.05.25 16.37.29
php.exe      3 220 K
1 917 952 mysqlitedb.db
---------------
2010.05.25 16.40.29
php.exe      3 368 K
2 070 528 mysqlitedb.db
---------------
2010.05.25 16.43.29
php.exe      3 368 K
2 197 504 mysqlitedb.db
---------------
2010.05.25 16.46.30
php.exe      3 368 K
2 320 384 mysqlitedb.db
---------------
2010.05.25 16.49.30
php.exe      3 368 K
2 437 120 mysqlitedb.db
---------------
2010.05.25 16.52.31
php.exe      3 368 K
2 549 760 mysqlitedb.db
---------------
2010.05.25 16.55.32
php.exe      3 368 K
2 658 304 mysqlitedb.db
---------------
2010.05.25 16.58.32
php.exe      3 368 K
2 763 776 mysqlitedb.db
---------------
2010.05.25 17.01.33
php.exe      3 368 K
2 868 224 mysqlitedb.db
---------------
2010.05.25 17.04.33
php.exe      3 368 K
2 965 504 mysqlitedb.db
---------------




I found on sqlite.org function sqlite3_soft_heap_limit(int) that limits the amount of heap memory that may be allocated by SQLite. But there is no such function in PHP SQLite extension.

So, I don't think that this is a bug. Sorry for the panic :)
 [2010-09-20 14:47 UTC] iliaa@php.net
-Status: Open +Status: Bogus
 [2010-09-20 14:47 UTC] iliaa@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

sqlite related.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Sun Dec 08 22:01:27 2024 UTC