I have a php script which apparently has a memory leak. Here is the relevant code:
$query = GetReallyBigQuery();
print '<br>before SQL memory usage=' . memory_get_usage();
$rs = SQL($query);
$iusercnt = -1; // used to show no rows
if ($rs)
{
print '<br>after SQL memory usage=' . memory_get_usage();
$ix = 0;
while ($row = mysqli_fetch_row($rs))
{
print '<br>ix=' . $ix++ . ' memory usage=' . memory_get_usage();
$auser[] = $row;
}
There are three print statements that show the current memory usage. The following is the output of these print statements for the first few iterations:
before SQL memory usage=2753368
after SQL memory usage=2753368
ix=0 memory usage=2753616
ix=1 memory usage=2753912
ix=2 memory usage=2754208
ix=3 memory usage=2754504
ix=4 memory usage=2754824
ix=5 memory usage=2755144
ix=6 memory usage=2756712
ix=7 memory usage=2759272
ix=8 memory usage=2762040
ix=9 memory usage=2764816
ix=10 memory usage=2767576
ix=11 memory usage=2770336
ix=12 memory usage=2773072
ix=13 memory usage=2775832
ix=14 memory usage=2778600
ix=15 memory usage=2781336
As you can see, after the first ten or so rows, loading each row into the array takes from 2000 - 3000 bytes. There are about 10000 rows in the result set, so this runs out of memory way before completing. However, the data in the rows should be much smaller, with each row consisting of typically 150 bytes. The maximum size of the data in each row could be is about 400 bytes, as shown by the following schema (derived from selecting the data in a standalone CREATE TABLE tempname SELECT <the select statement>, and then using SHOW CREATE TABLE tempname)
HOWEVER, please note that for the vast majority of the rows, the data is much smaller than the maximum sizes.
CREATE TABLE `autoemail` (
`usertypeid` bigint(20) unsigned NOT NULL default '0',
`userid` mediumint(8) unsigned NOT NULL default '0',
`dateentry` datetime default NULL,
`dateupdate` datetime default NULL,
`referrerid` mediumint(8) unsigned default NULL,
`referralcnt` smallint(6) unsigned default NULL,
`phone1` varchar(25) default NULL,
`email1` varchar(71) default NULL,
`status` char(1) default NULL,
`datestatus` datetime default NULL,
`FName` varchar(30) default NULL,
`Password` varchar(15) NOT NULL default '',
`LName` varchar(30) NOT NULL default '',
`Address1` varchar(60) NOT NULL default '',
`Address2` varchar(60) NOT NULL default '',
`City` varchar(30) NOT NULL default '',
`State` char(2) NOT NULL default '',
`Postal` varchar(10) NOT NULL default ''
) ENGINE=MyISAM DEFAULT CHARSET=latin1
The bottom line: each iteration through the loop adds about 200 bytes of data, yet the memory_get_usage function shows 2000-3000 bytes consumed.
What is causing this memory leak?
Start Free Trial