Memory consumption is one of the most common issues developers face when handling big data. If a program uses too much memory, it may cause the server to crash or slow down, and in severe cases, exceed the PHP script’s memory limit, causing the script to terminate. Therefore, it is crucial to monitor the memory usage of PHP programs in real time, and the memory_get_usage() function provides us with a simple and effective way to get the current memory usage of the script.
memory_get_usage() is a built-in PHP function that returns the current memory consumption of the PHP script. Its return value is in bytes, representing the total amount of memory allocated to the current script. You can use this value to monitor the program’s memory usage and ensure it does not exceed expectations.
<span><span><span class="hljs-variable">$memoryUsage</span></span><span> = </span><span><span class="hljs-title function_ invoke__">memory_get_usage</span></span>();
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Current memory usage: "</span></span><span> . </span><span><span class="hljs-variable">$memoryUsage</span></span><span> . </span><span><span class="hljs-string">" bytes"</span></span><span>;
</span></span>
When handling big data, especially during operations that may consume large amounts of memory—such as database queries, file reads, or large-scale data computations—real-time monitoring of memory changes becomes particularly important. We can combine memory_get_usage() with logging, debugging information, or timed outputs to observe fluctuations in memory usage.
<span><span><span class="hljs-comment">// Print initial memory usage</span></span><span>
</span><span><span class="hljs-keyword">echo</span></span><span> </span><span><span class="hljs-string">"Initial memory usage: "</span></span><span> . </span><span><span class="hljs-title function_ invoke__">memory_get_usage</span></span><span>() . </span><span><span class="hljs-string">" bytes\n"</span></span><span>;
</span><span><span class="hljs-comment">// Simulate big data operation: generate large data</span></span><span>
</span><span><span class="hljs-variable">$data</span></span> = [];
</span><span><span class="hljs-keyword">for</span> (</span><span><span class="hljs-variable">$i</span></span> = </span><span>0; </span><span><span class="hljs-variable">$i</span></span> < 100000; </span><span><span class="hljs-variable">$i</span></span>++) {
</span><span><span class="hljs-variable">$data</span></span>[] = </span><span><span class="hljs-title function_ invoke__">str_repeat</span>('a', 1024); </span><span><span class="hljs-comment">// Each string occupies 1024 bytes</span>
}
</span><span><span class="hljs-comment">// Print memory usage after data operation</span>
</span><span><span class="hljs-keyword">echo</span> "Memory usage after processing data: " . memory_get_usage() . " bytes\n";
</span><span><span class="hljs-comment">// Simulate other memory operations</span>
</span><span><span class="hljs-keyword">unset</span>($data); </span><span><span class="hljs-comment">// Clear data</span>
</span><span><span class="hljs-keyword">echo</span> "Memory usage after clearing data: " . memory_get_usage() . " bytes\n";
</span>
In this example, memory usage is printed at different operation points. This way, we can observe how memory consumption changes during execution.
Reduce Unnecessary Data Storage
When handling large amounts of data, avoid storing unnecessary data in memory. Use database queries and pagination to load data in batches, preventing excessive data loading at once.
Use Memory-Mapped Files
For very large files, use functions like fopen() and fread() to read line by line instead of loading the entire file into memory at once.
Regularly Clean Up Variables
Use unset() to clear variables that are no longer needed, which helps free up occupied memory.
Memory Limit Configuration
You can set memory limits through the PHP configuration file or within scripts using ini_set('memory_limit', '256M'). Adjust memory limits timely during program execution to avoid memory overflow.
Use Memory-Optimized Data Structures
Employ appropriate memory-optimized algorithms and data structures (for example, using SplFixedArray instead of regular arrays) to effectively reduce memory consumption.
PHP provides a garbage collection mechanism (GC) that automatically recycles unused memory. However, when dealing with large data, garbage collection may not immediately free all unused memory. In such cases, you can manually trigger garbage collection and use memory_get_usage() to observe memory changes before and after collection.
<span><span><span class="hljs-keyword">echo</span></span> "Memory usage before: " . memory_get_usage() . " bytes\n";
</span><span><span class="hljs-comment">// Simulate large memory consumption operation</span>
</span><span><span class="hljs-variable">$data</span> = str_repeat('a', 1024 * 1024 * 50); </span><span><span class="hljs-comment">// 50MB data</span>
</span><span><span class="hljs-comment">// Manually trigger garbage collection</span>
</span><span><span class="hljs-title function_ invoke__">gc_collect_cycles</span>();
</span><span><span class="hljs-keyword">echo</span> "Memory usage after: " . memory_get_usage() . " bytes\n";
</span>
By using the memory_get_usage() function, we can monitor the memory consumption of PHP scripts in real time and take corresponding measures to optimize memory usage, ensuring the program does not exceed memory limits when processing big data. In addition, combined with other memory optimization techniques, we can effectively manage memory resources to avoid memory overflow and performance degradation.