Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Tracking Memory Usage in PHP
The memory_get_usage() function can be used to track the memory usage in PHP. The 'malloc' function is not used for every block required, instead a big chunk of system memory is allocated and the environment variable is changed and managed internally.
The two different types of memory usages are −
- The memory required by the engine from OS (the real usage)
- The amount of memory that was actually used by the application (internal usage)
The above mentioned memory usage can be tracked using memory_get_usage(). This function returns both real and actual memory used depending on our requirement.
For example: if we are looking at specific code snippets, internal memory might be relevant. On the other hand, if memory usage is being globally tracked, the real usage would be more relevant.
Syntax
memory_get_usage(bool $real_usage = false)
Parameters
$real_usage − If set to true, returns the real memory allocated from the system. If false (default), returns only the memory used by the emalloc() function.
Example 1: Basic Memory Usage
Let's track memory usage before and after creating an array ?
<?php
// Get initial memory usage
$initial_memory = memory_get_usage();
echo "Initial memory usage: " . $initial_memory . " bytes<br>";
// Create a large array
$data = array();
for ($i = 0; $i < 10000; $i++) {
$data[] = "Item " . $i;
}
// Check memory after creating array
$after_memory = memory_get_usage();
echo "Memory after array creation: " . $after_memory . " bytes<br>";
echo "Memory difference: " . ($after_memory - $initial_memory) . " bytes<br>";
?>
Example 2: Real vs Internal Memory Usage
Compare real memory usage with internal memory usage ?
<?php // Get internal memory usage (default) $internal_memory = memory_get_usage(false); echo "Internal memory usage: " . $internal_memory . " bytes<br>"; // Get real memory usage $real_memory = memory_get_usage(true); echo "Real memory usage: " . $real_memory . " bytes<br>"; echo "Difference: " . ($real_memory - $internal_memory) . " bytes<br>"; ?>
Memory Tracking Function
You can also use memory_get_peak_usage() to get the peak memory usage during script execution ?
<?php
function formatBytes($bytes) {
if ($bytes >= 1048576) {
return round($bytes / 1048576, 2) . ' MB';
} elseif ($bytes >= 1024) {
return round($bytes / 1024, 2) . ' KB';
} else {
return $bytes . ' bytes';
}
}
echo "Current memory usage: " . formatBytes(memory_get_usage()) . "<br>";
echo "Peak memory usage: " . formatBytes(memory_get_peak_usage()) . "<br>";
echo "Current real memory usage: " . formatBytes(memory_get_usage(true)) . "<br>";
echo "Peak real memory usage: " . formatBytes(memory_get_peak_usage(true)) . "<br>";
?>
Conclusion
PHP's memory_get_usage() function is essential for monitoring memory consumption. Use the $real_usage parameter to choose between internal memory tracking and actual system memory allocation based on your monitoring needs.
