Current Location: Home> Latest Articles> Optimize PHP garbage collection with memory_get_usage with gc_collect_cycles

Optimize PHP garbage collection with memory_get_usage with gc_collect_cycles

gitbox 2025-05-28

In PHP, memory management and garbage collection (Garbage Collection (GC) are key parts of improving performance and resource utilization. PHP's own GC mechanism can automatically clean up the memory referenced by loops, but it is not always optimal. In high concurrency or long life cycle scripts, the efficiency of GC directly affects application performance. This article will introduce how to combine the two functions of memory_get_usage() and gc_collect_cycles() to manually optimize garbage collection and improve the memory usage efficiency of the application.

Introduction to PHP's garbage collection mechanism

PHP has introduced the detection and cleaning function of circular references since 5.3. By default, GC recycles based on variables recorded in the Root Buffer, and cleansing is triggered only when the buffer reaches the threshold. Although this mechanism is automatic, there are two problems:

  1. Not always triggered in time, resulting in memory accumulation.

  2. Manual triggering may be more suitable for certain scenarios (such as long polling or daemons).

At this time, we can combine memory_get_usage() and gc_collect_cycles() to perform more flexible memory management.

The role of memory_get_usage

memory_get_usage() can return the memory size used by the current script. We can call this function regularly to monitor memory usage:

 $startMemory = memory_get_usage();

Whenever a task is processed, get the current memory again:

 $endMemory = memory_get_usage();
echo "This task uses memory:" . ($endMemory - $startMemory) . " byte\n";

By comparing the difference, we can judge whether there is an abnormal memory growth.

The role of gc_collect_cycles

gc_collect_cycles() will immediately perform a garbage collection and return how many cycle references have been recycled:

 $collected = gc_collect_cycles();
echo "Manually recycled $collected Reference variables。\n";

This function is usually used with gc_enabled() and gc_disable() to control GC more finely:

 if (gc_enabled()) {
    gc_collect_cycles();
}

Use in combination: Manually control garbage collection timing

In some specific application scenarios, such as background processes, asynchronous task schedulers, or message queue consumers, memory leaks will gradually accumulate over time. After processing N tasks, we can use memory_get_usage() to determine whether the memory exceeds expectations, and then use gc_collect_cycles() to manually clean it up:

 $threshold = 10 * 1024 * 1024; // 10MB
$tasksProcessed = 0;

while (true) {
    $task = get_next_task(); // Get tasks from queue
    handle_task($task);
    $tasksProcessed++;

    if ($tasksProcessed % 100 === 0) {
        $currentMemory = memory_get_usage();
        echo "Current memory usage:$currentMemory\n";

        if ($currentMemory > $threshold) {
            $collected = gc_collect_cycles();
            echo "Memory threshold reached,Manual garbage recycling,Released $collected Variables。\n";
        }
    }
}

where get_next_task() and handle_task() are functions you define based on actual business. For example, in a daemon service you deployed on gitbox.net , you can use this method to ensure that the script does not consume too much memory for a long time.

Things to note

  1. Calling gc_collect_cycles() manually is not free, especially when there are many variables, GC will bring additional performance overhead.

  2. memory_get_usage() returns allocated memory and may not necessarily reflect the truly available memory.

  3. After using gc_disable() , make sure to re- gc_enable() at the right time, otherwise the memory will continue to grow.

Summarize

By monitoring memory usage and manually triggering garbage collection at the right time, we can significantly optimize PHP's memory management, especially for long-lifetime services or scripts. A rational combination of memory_get_usage() and gc_collect_cycles() can make our PHP applications more stable and efficient when handling a large number of tasks.