In PHP development, performance optimization and resource management are very important links. Understanding how much memory the script occupies during runtime can help developers discover memory leaks or unreasonable memory usage in a timely manner, thereby performing targeted optimization. PHP's built-in memory_get_usage function is a powerful tool to monitor the current script memory usage. This article will explain in detail the usage of this function and help you master its usage skills based on actual application scenarios.
memory_get_usage is a built-in function provided by PHP to return the number of memory bytes occupied by the current script. Its basic syntax is as follows:
memory_get_usage(bool $real_usage = false): int
The $real_usage parameter is a boolean value, defaulting to false .
When false , the function returns the memory size applied to the script by PHP, which does not necessarily mean the physical memory that is actually allocated to the script.
When true , the memory size actually allocated to the PHP script is returned, which may be larger than the default return value.
<?php
echo "Initial memory usage: " . memory_get_usage() . " byte\n";
$array = range(1, 10000);
echo "Memory usage after creating an array: " . memory_get_usage() . " byte\n";
?>
This code first outputs the initial memory usage of the script, then creates an array of 10,000 integers, and finally outputs the current memory usage.
In large applications, complex loops may be performed or large amounts of data are loaded. Timely monitoring memory usage can prevent scripts from crashing due to memory exhaustion.
<?php
echo "Start memory usage of scripts: " . memory_get_usage() . " byte\n";
for ($i = 0; $i < 1000; $i++) {
$data = str_repeat("A", 10000);
if ($i % 100 === 0) {
echo "1. {$i} Second loop memory usage: " . memory_get_usage() . " byte\n";
}
}
echo "End memory usage by script: " . memory_get_usage() . " byte\n";
?>
By calling memory_get_usage before and after the key code segment, the impact of different writing methods or data structures on memory can be compared, thereby performing targeted optimization.
PHP also has a similar function memory_get_peak_usage , which is used to obtain the maximum memory peak used during script running. It also accepts a $real_usage parameter.
<?php
echo "Memory peak value: " . memory_get_peak_usage() . " byte\n";
?>
By combining the use of both, developers can better understand the usage of memory:
<?php
echo "Initial memory: " . memory_get_usage() . " byte\n";
$data = [];
for ($i = 0; $i < 10000; $i++) {
$data[] = md5($i);
}
echo "Current memory: " . memory_get_usage() . " byte\n";
echo "Memory peak value: " . memory_get_peak_usage() . " byte\n";
?>
memory_get_usage() is used to get the memory usage of the current script.
Through it, memory consumption can be monitored and analyzed and potential performance issues can be located.
With memory_get_peak_usage() , you can understand the maximum memory consumption peak.
In large-scale projects and resource-sensitive projects, the rational use of these two functions is of great significance to performance optimization.
<?php
echo "Initial memory usage: " . memory_get_usage() . " byte\n";
$array = range(1, 10000);
echo "Memory usage after creating an array: " . memory_get_usage() . " byte\n";
for ($i = 0; $i < 1000; $i++) {
$data = str_repeat("A", 10000);
if ($i % 100 === 0) {
echo "1. {$i} Second loop memory usage: " . memory_get_usage() . " byte\n";
}
}
echo "Memory peak value: " . memory_get_peak_usage() . " byte\n";
?>