Use gc_status() when you need to returns detailed statistics about PHP cyclic garbage collection in a PHP application. The examples below show the actual operation and its limitations.
What Is gc_status() in PHP?
Returns detailed statistics about PHP cyclic garbage collection. The exact signature supported by your installation depends on its PHP version and enabled extensions.
Its syntax is:
gc_status(): array
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
$status = gc_status(); echo isset($status["runs"]) ? "Statistics 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
print_r(array_intersect_key(gc_status(), array_flip(["runs","collected"])));
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
$before = gc_status(); gc_collect_cycles(); $after = gc_status(); echo "Runs recorded: " . $after["runs"];
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
Statistics reflect the current process and should not be interpreted as global server metrics.
Comparison With Related Functions
| Approach | Difference |
|---|---|
gc_status() | Returns detailed statistics about PHP cyclic garbage collection. |
| Related operation | gc_collect_cycles() performs a collection; gc_status() only reports metrics. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring the function-specific limitation | Statistics reflect the current process and should not be interpreted as global server metrics. |
| Confusing this function with a related operation | gc_collect_cycles() performs a collection; gc_status() only reports metrics. |
Key Takeaways
- Returns detailed statistics about PHP cyclic garbage collection.
- Statistics reflect the current process and should not be interpreted as global server metrics.
- gc_collect_cycles() performs a collection; gc_status() only reports metrics.
Use gc_status() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.