Sometimes memory_get_usage()
return a bigger value after using free()
. See the example below:
$query = "SELECT * FROM table";
$result = self::query($query);
while ($row = $result->fetch_assoc())
{
$myArray[] = $row;
}
echo "Before: ".number_format(memory_get_usage());
$result->free();
echo "After: ".number_format(memory_get_usage());
The output is:
Before: 1,203,856
After: 1,370,976
But if I comment the instruction in the loop then the memory usage decrease after the free()
:
$result = self::query($query);
while ($row = $result->fetch_assoc())
{
//$myArray[] = $row;
}
echo "Before: ".number_format(memory_get_usage());
$result->free();
echo "After: ".number_format(memory_get_usage());
Gives:
Before: 593,120
After: 325,448
The result is quite big (~Mb)
and there is a lot of text. It' only a few hundreds lines.
If the result has less than ~10 lines the memory usage always decrease.
My guess was that free() free the memory of MySQL and not php but then why does it usually decrease the php memory usage?
Can somebody explains what's going on here?