View the amount of memory allocated to PHP, unset() analysis of the amount of memory actually used by PHP scripts
1. Reference URL:https://www.php.net/manual/zh/function.memory-get-usage.php
2. Based on an example of memory_get_usage(), after adjustment, as shown in Figure 1, the code is as follows:
//这只是个例子,下面的数字取决于你的系统
echo memory_get_usage()/1024/1024 . " MB\n"; // 0.37361145019531 MB
$a = str_repeat("Hello", 424200);
echo memory_get_usage()/1024/1024 . " MB\n"; // 4.3736343383789 MB
unset($a);
echo memory_get_usage()/1024/1024 . " MB\n"; // 0.37361145019531 MB
echo memory_get_peak_usage()/1024/1024 . " MB\n"; // 4.3739242553711 MB
3. The execution result, unset() is effective for reducing the amount of memory actually used by the php script.
0.37361145019531 MB 4.3736343383789 MB 0.37361145019531 MB 4.3739242553711 MB
4. In Yii 2.0, run the same code, check the memory usage of the debug data, its value is memory 11.356 MB, and memory_get_peak_usage() – return assigned to php The peaks of the memory are consistent. as shown in Figure 2
7.3560180664062 MB 11.35604095459 MB 7.3560180664062 MB 11.356330871582 MB
5. Repeat step 2, set real_usage to true, and get the total memory size allocated by the system, including unused pages. If it is not set or set to False, only the amount of memory used is reported. Execution result, unset() is still effective for decreasing the total memory allocated by the php script to obtain the system allocated by the system.
// 这只是个例子,下面的数字取决于你的系统
echo memory_get_usage(true)/1024/1024 . " MB\n"; // 0.37361145019531 MB
$a = str_repeat("Hello", 424200);
echo memory_get_usage(true)/1024/1024 . " MB\n"; // 4.3736343383789 MB
unset($a);
echo memory_get_usage(true)/1024/1024 . " MB\n"; // 0.37361145019531 MB
echo memory_get_peak_usage(true)/1024/1024 . " MB\n"; // 4.3739242553711 MB
2 MB 6 MB 2 MB 6 MB

