Monitoring PHP Script Memory and CPU Consumption

When optimizing applications, understanding resource consumption is critical. PHP provides built-in functions to inspect how much memory and CPU time a script consumse during execution.

Tracking Memory Usage

PHP handles memory management automatically, but you can still monitor allocation in real-time. Use memory_get_usage() to check the current memory consumption and memory_get_peak_usage() to find the highest amount allocated during the script's lifecycle.

echo "Start: " . memory_get_usage() . " bytes\n";

$container = [];
for ($x = 0; $x < 100000; $x++) {
    $container[] = sha1($x);
}

// Remove the generated data
for ($x = 0; $x < 100000; $x++) {
    unset($container[$x]);
}

echo "End: " . memory_get_usage() . " bytes\n";
echo "Peak: " . memory_get_peak_usage() . " bytes\n";

Analyzing CPU Usage

The getrusage() function returns CPU usage data. Note that this function is not available on Windows platforms.

print_r(getrusage());

The returned array contains specific keys regarding resource usage. Here is a breakdown of the most relevant metrics:

  • ru_utime.tv_sec / ru_utime.tv_usec: Time spent in user-mode (seconds and microseconds).
  • ru_stime.tv_sec / ru_stime.tv_usec: Time spent in kernel-mode (seconds and microseconds).
  • ru_maxrss: Maximum resident set size (memory).
  • ru_nvcsw: Number of voluntary context switches.
  • ru_nivcsw: Number of involuntary context switches.
  • ru_minflt / ru_majflt: Page reclaims and page faults.

To calculate the total execution time in seconds, you combine the seconds and microseconds values:

// Non-CPU intensive wait
sleep(3);

$stats = getrusage();
$userTime = $stats['ru_utime.tv_sec'] + ($stats['ru_utime.tv_usec'] / 1000000);
$systemTime = $stats['ru_stime.tv_sec'] + ($stats['ru_stime.tv_usec'] / 1000000);

echo "User Time: " . $userTime . "\n";
echo "System Time: " . $systemTime . "\n";

In the example above, sleep() does not consume CPU cycles, so the user time remains low. Contrast this with a busy loop:

// Busy loop calculation
for ($counter = 0; $counter < 10000000; $counter++) {
    // Empty loop
}

$stats = getrusage();
$userTime = $stats['ru_utime.tv_sec'] + ($stats['ru_utime.tv_usec'] / 1000000);
echo "User Time: " . $userTime . "\n";

System time reflects kernel-level operatoins. The following example forces both user and system time consumption by calling a system function repeatedly within a loop:

$startTime = microtime(true);

// Busy loop calling a system function
while (microtime(true) - $startTime < 3) {
    // Loop relies on system time retrieval
}

$stats = getrusage();
$userTime = $stats['ru_utime.tv_sec'] + ($stats['ru_utime.tv_usec'] / 1000000);
$systemTime = $stats['ru_stime.tv_sec'] + ($stats['ru_stime.tv_usec'] / 1000000);

echo "User Time: " . $userTime . "\n";
echo "System Time: " . $systemTime . "\n";

Tags: PHP memory_get_usage getrusage CPU profiling performance monitoring

Posted on Tue, 13 Oct 2026 16:33:59 +0000 by mr.rum