filesystem icon indicating copy to clipboard operation
filesystem copied to clipboard

uv memory leak

Open wpjscc opened this issue 4 months ago • 6 comments

<?php

require __DIR__ . '/vendor/autoload.php';

use React\Filesystem\Factory;
use function React\Async\async;
use function React\Async\delay;
use React\EventLoop\Loop;

Loop::addPeriodicTimer(1, function () {
    echo 'current_memory_usage: ' . round(memory_get_usage() / 1024 / 1024, 3) . "\n";
});

$filesystem = Factory::create();


async(function () use ($filesystem) {
    while (true) {
        delay(1);
        $filesystem->file(__DIR__ . '/test.txt')->putContents(
            json_encode([
                'current_usage_mb' => round(memory_get_usage() / 1024 / 1024, 3),
                'current_usage_real_mb' => round(memory_get_usage(true) / 1024 / 1024, 3),
                'peak_usage_mb' => round(memory_get_peak_usage() / 1024 / 1024, 3),
                'peak_usage_real_mb' => round(memory_get_peak_usage(true) / 1024 / 1024, 3),
                'memory_limit' => ini_get('memory_limit'),
            ], JSON_PRETTY_PRINT),
            \FILE_APPEND
        );
    }
})();

output

current_memory_usage: 0.921
current_memory_usage: 0.921
current_memory_usage: 0.921
current_memory_usage: 0.922
current_memory_usage: 0.922
current_memory_usage: 0.923
current_memory_usage: 0.923
current_memory_usage: 0.924
current_memory_usage: 0.924
current_memory_usage: 0.924
current_memory_usage: 0.925
current_memory_usage: 0.925
current_memory_usage: 0.926
current_memory_usage: 0.926
current_memory_usage: 0.927
current_memory_usage: 0.927
current_memory_usage: 0.927
current_memory_usage: 0.928
current_memory_usage: 0.932
current_memory_usage: 0.933
current_memory_usage: 0.933
current_memory_usage: 0.933
current_memory_usage: 0.934
current_memory_usage: 0.934
current_memory_usage: 0.935
current_memory_usage: 0.935
current_memory_usage: 0.936
current_memory_usage: 0.936
current_memory_usage: 0.936
current_memory_usage: 0.937

wpjscc avatar Oct 26 '25 13:10 wpjscc

<?php

require __DIR__ . '/vendor/autoload.php';

use React\Filesystem\Factory;
use function React\Async\async;
use function React\Async\delay;
use React\EventLoop\Loop;

Loop::addPeriodicTimer(1, function () {
    echo 'current_memory_usage: ' . round(memory_get_usage() / 1024 / 1024, 3) . "\n";
});

$filesystem = Factory::create();

function getMemoryUsage() {
    return [
        'date' => date('Y-m-d H:i:s'),
        'current_usage_mb' => round(memory_get_usage() / 1024 / 1024, 3),
        'current_usage_real_mb' => round(memory_get_usage(true) / 1024 / 1024, 3),
        'peak_usage_mb' => round(memory_get_peak_usage() / 1024 / 1024, 3),
        'peak_usage_real_mb' => round(memory_get_peak_usage(true) / 1024 / 1024, 3),
    ];
}

$file = $filesystem->file(__DIR__ . '/test.txt');
async(function () use ($file) {

    $startMemoryUsage = getMemoryUsage();

    while (true) {
        delay(1);
        $file->putContents(
            json_encode([
                'start_memory_usage' => $startMemoryUsage,
                'current_memory_usage' => getMemoryUsage(),
            ], JSON_PRETTY_PRINT),
            // \FILE_APPEND
        )->then(function () {
            echo 'file written' . "\n";
        });
    }
})();

test.text

{
    "start_memory_usage": {
        "date": "2025-10-27 02:33:28",
        "current_usage_mb": 0.84,
        "current_usage_real_mb": 2,
        "peak_usage_mb": 0.866,
        "peak_usage_real_mb": 2
    },
    "current_memory_usage": {
        "date": "2025-10-27 05:21:08",
        "current_usage_mb": 5.788,
        "current_usage_real_mb": 8,
        "peak_usage_mb": 5.807,
        "peak_usage_real_mb": 8
    }
}

The memory has been continuously increasing for three hours.

wpjscc avatar Oct 27 '25 05:10 wpjscc

The memory has been continuously increasing for four hours.

{
    "start_memory_usage": {
        "date": "2025-10-27 02:33:28",
        "current_usage_mb": 0.84,
        "current_usage_real_mb": 2,
        "peak_usage_mb": 0.866,
        "peak_usage_real_mb": 2
    },
    "current_memory_usage": {
        "date": "2025-10-27 06:38:43",
        "current_usage_mb": 7.774,
        "current_usage_real_mb": 10,
        "peak_usage_mb": 7.794,
        "peak_usage_real_mb": 10
    }
}

wpjscc avatar Oct 27 '25 06:10 wpjscc

The memory has been continuously increasing for Seven hours.

{
    "start_memory_usage": {
        "date": "2025-10-27 02:33:28",
        "current_usage_mb": 0.84,
        "current_usage_real_mb": 2,
        "peak_usage_mb": 0.866,
        "peak_usage_real_mb": 2
    },
    "current_memory_usage": {
        "date": "2025-10-27 09:42:04",
        "current_usage_mb": 13.095,
        "current_usage_real_mb": 14,
        "peak_usage_mb": 13.114,
        "peak_usage_real_mb": 14
    }
}

wpjscc avatar Oct 27 '25 09:10 wpjscc

Is this only when writing, or also when doing other operations?

WyriHaximus avatar Nov 05 '25 20:11 WyriHaximus

Only write operations were tested, and when using amphp/file for testing, the same memory leak occurred. Therefore, I suspect it might be an issue within the uv extension.

wpjscc avatar Nov 08 '25 05:11 wpjscc

It might also be happening when reading and listing directory contents. Been having a similar memory leak in one of my apps that very slowly, like this one, increases. But haven't been able to figure out where it happens, and in what component. Will run it without ext-uv next weekend and see what happens.

WyriHaximus avatar Nov 10 '25 10:11 WyriHaximus