Current Location: Home> Latest Articles> How to Use memory_get_usage to Monitor PHP Memory Consumption in Real Time When Handling Big Data

How to Use memory_get_usage to Monitor PHP Memory Consumption in Real Time When Handling Big Data

M66 2025-06-28

When handling big data, memory consumption is one of the most common challenges developers face. If a program uses too much memory, it may cause the server to crash or degrade performance, and in severe cases, it can exceed the PHP script’s memory limit, causing the script to terminate. Therefore, monitoring PHP’s memory usage in real time is very important, and the memory_get_usage() function offers a simple and effective way to obtain the amount of memory currently used by the script.

1. Introduction to the memory_get_usage() Function

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 memory allocated to the script at that moment. You can use this value to monitor your program’s memory usage and ensure it does not exceed expectations.

$memoryUsage = memory_get_usage();
echo "Current memory usage: " . $memoryUsage . " bytes";

2. How to Use memory_get_usage() to Monitor Memory in Real Time

When handling big data, especially when performing memory-intensive operations such as database queries, file reading, or large-scale data calculations, monitoring memory usage changes in real time becomes especially important. We can combine memory_get_usage() with logging, debugging information, or timed outputs to observe fluctuations in memory consumption.

Example: Real-Time Memory Consumption Monitoring

// Print initial memory usage
echo "Initial memory usage: " . memory_get_usage() . " bytes\n";
// Simulate big data operation: generate large amount of data
$data = [];
for ($i = </span>0; </span>$i < </span>100000; </span>$i++) {
    </span>$data[] = </span>str_repeat('a', </span>1024);  // Each string occupies 1024 bytes
}
</span>// Print memory usage after data operation
echo "Memory usage after processing data: " . </span>memory_get_usage() . </span>" bytes\n";
// Simulate other memory operations
unset($data);  // Clear data
echo "Memory usage after clearing data: " . </span>memory_get_usage() . </span>" bytes\n";
</span>

In this example, memory usage is printed at different operation points. This allows us to observe how memory consumption changes throughout the process.

3. Tips for Optimizing Memory Usage

  1. Reduce unnecessary data storage
    When dealing with large data volumes, avoid storing unnecessary data in memory. Use database queries and pagination to load data in batches instead of loading everything at once.

  2. 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.

  3. Regularly clear variables
    Use unset() to remove variables that are no longer needed to free up occupied memory.

  4. Memory limit configuration
    You can set memory limits via the PHP configuration file or in the script using ini_set('memory_limit', '256M'). Adjust memory limits during runtime as needed to prevent memory overflow.

  5. Use memory-optimized data structures
    Use appropriate memory-efficient algorithms and data structures (for example, using SplFixedArray instead of regular arrays) to reduce memory consumption effectively.

4. Combining memory_get_usage() with gc_collect_cycles()

PHP provides a garbage collection (GC) mechanism that automatically frees memory that is no longer in use. However, when processing large amounts of data, garbage collection might not immediately clean up all unused memory. In this case, you can manually trigger garbage collection and use memory_get_usage() to observe memory changes before and after collection.

echo "Memory usage before: " . memory_get_usage() . </span>" bytes\n";
// Simulate large memory consumption
$data = </span>str_repeat('a', </span>1024 * 1024 * 50);  </span>// 50MB data
</span>// Manually trigger garbage collection
gc_collect_cycles();
echo "Memory usage after: " . </span>memory_get_usage() . </span>" bytes\n";
</span>

5. Conclusion

By using the memory_get_usage() function, we can monitor the PHP script’s memory consumption in real time and take corresponding measures to optimize memory usage, ensuring the program does not exceed memory limits when handling big data. Additionally, by combining other memory optimization techniques, we can effectively manage memory resources and avoid memory overflow and performance degradation.