Memory_get_usage

I am doing a small class of tests to display page load time and memory usage. The boot time already works, but when I show the memory usage, it does not change Example:

$conns = array();
ob_start();
benchmark::start();
$conns[] = mysql_connect('localhost', 'root', '');
benchmark::stop();
ob_flush();

uses the same memory as

$conns = array();
ob_start();
benchmark::start();
for($i = 0; $i < 1000; $i++)
{
   $conns[] = mysql_connect('localhost', 'root', '');
}
benchmark::stop();
ob_flush();

I use memory_get_usage (true) to get memory usage in bytes.

+5
source share
4 answers

memory_get_usage(true)will display the amount of memory allocated by the php processor, not the script used. It is very possible that your test script did not require the engine to request more memory.

For the test, take a large (ish) file and read it in memory. Then you should see the change.

memory_get_usage(true) -, ( , ). , , , , , . , , ( ).

real_usage false, . php, .

( . , , , - , script, , script. script, .)

+8

PHP-, , echo , PHP, - .

, .

:

$result = null;
benchmark::start()
for($i = 0; $i < 10000; $i++)
{
   $result.='test';
}
+2

:

for($i = 0; $i < 1000; $i++)
{
   $conns[] = mysql_connect('localhost', 'root', '');
}

100 000, , . , , , . ? () $conns[0]. memory_get_usage(). $conns[15], , , ?

root @localhost ? . PHP , ? ( ).

CLI Valgrind, :

valgrind /usr/bin/php -f foo.php.. - . , , .

Disclaimer: I know my way around the internal components of PHP, but I am not an expert in this intentionally confusing maze written in C that Zend calls PHP.

+2
source

echo will not change the allocated number of bytes (unless you use output buffers ).

the $ i variable does not execute after the for loop, so it does not change the number of bytes allocated.

try using the output buffering example:

ob_start();
benchmark::start();
for($i = 0; $i < 10000; $i++)
{
   echo 'test';
}
benchmark::stop();
ob_flush();
0
source

All Articles