Use getrusage() when you need to returns operating-system resource statistics for a process in a PHP application. The examples below show the actual operation and its limitations.
What Is getrusage() in PHP?
Returns operating-system resource statistics for a process. The exact signature supported by your installation depends on its PHP version and enabled extensions.
Its syntax is:
getrusage(int $mode = ...): array|false
Parameters and return value: The parameter types, optional arguments, references, and return type are shown in the signature above. Optional parameters are marked with an ellipsis in this reference signature.
Example 1: Basic Usage
<?php
$stats = function_exists("getrusage") ? getrusage() : false; echo is_array($stats) ? "Stats available" : "Unavailable";
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Example 2: Different Input or Behavior
<?php
$usage = function_exists("getrusage") ? getrusage() : false; var_dump(is_array($usage));
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Practical Example: Applying the Function
<?php
$usage = function_exists("getrusage") ? getrusage() : false; if (is_array($usage)) echo "Fields: " . count($usage);
Result: Run the example in an environment that supports the function. Any values depending on operating-system state, installed extensions, process configuration, or network access must be checked on the target server.
Important Behavior and Edge Cases
Field units and availability vary across operating systems; not available on every platform.
Comparison With Related Functions
| Approach | Difference |
|---|---|
getrusage() | Returns operating-system resource statistics for a process. |
| Related operation | memory_get_usage() reports PHP memory use; getrusage() reports OS resource statistics. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring the function-specific limitation | Field units and availability vary across operating systems; not available on every platform. |
| Confusing this function with a related operation | memory_get_usage() reports PHP memory use; getrusage() reports OS resource statistics. |
Key Takeaways
- Returns operating-system resource statistics for a process.
- Field units and availability vary across operating systems; not available on every platform.
- memory_get_usage() reports PHP memory use; getrusage() reports OS resource statistics.
Use getrusage() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.