Use gc_disable() when you need to disables automatic cycle collection for the running request in a PHP application. The examples below show the actual operation and its limitations.
What Is gc_disable() in PHP?
Disables automatic cycle collection for the running request. The exact signature supported by your installation depends on its PHP version and enabled extensions.
Its syntax is:
gc_disable(): void
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
gc_disable(); var_dump(gc_enabled()); gc_enable();
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
gc_disable(); echo gc_enabled() ? "On" : "Off"; gc_enable();
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
$wasEnabled = gc_enabled(); gc_disable(); echo "Temporarily disabled"; if ($wasEnabled) gc_enable();
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
Disabling collection may increase memory use when cyclic references accumulate.
Comparison With Related Functions
| Approach | Difference |
|---|---|
gc_disable() | Disables automatic cycle collection for the running request. |
| Related operation | gc_enable() turns automatic cycle collection back on. |
Common Beginner Mistakes
| Mistake | Correct Approach |
|---|---|
| Ignoring the function-specific limitation | Disabling collection may increase memory use when cyclic references accumulate. |
| Confusing this function with a related operation | gc_enable() turns automatic cycle collection back on. |
Key Takeaways
- Disables automatic cycle collection for the running request.
- Disabling collection may increase memory use when cyclic references accumulate.
- gc_enable() turns automatic cycle collection back on.
Use gc_disable() for the specific operation demonstrated above. Check its prerequisites and handle failure conditions before using it with production data.