1. Reference URL: https://www.php.net/manual/zh/function.memory-get-usage.php
2. Based on a memory_get_usage() example, after adjustments, 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 shows that unset() is effective in reducing the actual amount of memory used by the PHP script.
0.37361145019531 MB 4.3736343383789 MB 0.37361145019531 MB 4.3739242553711 MB
4. In Yii 2.0, running the same code and checking the memory usage in the debug data shows a value of Memory 11.356 MB, which is consistent with memory_get_peak_usage() – returns the peak of memory allocated to PHP. As shown in Figure 2

7.3560180664062 MB 11.35604095459 MB 7.3560180664062 MB 11.356330871582 MB
5. Repeat step 2, setting real_usage to TRUE to get the total size of memory allocated by the system, including unused pages. If not set or set to FALSE, only the actual used memory amount is reported. The execution result shows that unset() is still effective in reducing the total size of memory allocated by the system for the PHP script.
// 这只是个例子,下面的数字取决于你的系统
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
