reactphp / filesystem

Evented filesystem access.
MIT License
135 stars 40 forks source link

Memory leak? #114

Closed ellisonpatterson closed 1 year ago

ellisonpatterson commented 1 year ago

I am using the latest dev-master for my application and am using it as a logger. It seems that there is some type of memory leak going on, as over time the memory continues to rise each time I write to a file. I am able to reproduce this with the following code sample.

What is going on, am I doing something wrong?

I am using the UV extension.

Thank you!

Test Script:

<?php

\define('ABSPATH', \dirname(__FILE__));
require ABSPATH . '/vendor/autoload.php';

use React\EventLoop\Loop;
use React\Filesystem\Factory as FileFactory;
use React\Filesystem\Node\NotExistInterface;

$fileFactory = FileFactory::create();
$fileFactory->detect(ABSPATH . '/test.log')->then(function($node) {
    if ($node instanceof NotExistInterface) {
        return $node->createFile();
    }

    return $node;
})->then(function($node) {
    Loop::get()->addPeriodicTimer(1.0, function() use($node) {
        $message = 'Memory usage: ' . memory_get_usage() . PHP_EOL;
        echo $message;

        $node->putContents($message, FILE_APPEND);
    });
});

Output:

php test-filesystem.php 
Memory usage: 5120560
Memory usage: 5121040
Memory usage: 5121488
Memory usage: 5121936
Memory usage: 5122704
Memory usage: 5123152
Memory usage: 5123600
Memory usage: 5124048
Memory usage: 5124496
Memory usage: 5124944
Memory usage: 5125392
Memory usage: 5125840
Memory usage: 5126928
Memory usage: 5127376
Memory usage: 5127824
Memory usage: 5128272
Memory usage: 5128720
Memory usage: 5129168
Memory usage: 5129616
Memory usage: 5130064
Memory usage: 5130512
Memory usage: 5130960
Memory usage: 5131408
Memory usage: 5131856
Memory usage: 5132304
Memory usage: 5132752
Memory usage: 5133200
Memory usage: 5133648
Memory usage: 5135376
Memory usage: 5135824
Memory usage: 5136272
Memory usage: 5136720
Memory usage: 5137168
SimonFrings commented 1 year ago

Thanks for bringing this up @ellisonpatterson, it's normal that memory increases while PHP is running. From what I get out of your example, it seems like the memory grows from 5,12 MB to to 5,13 MB and I would expect to fall back down after a certain amount of time/growth. I also answered a similar ticket in here: https://github.com/reactphp/http/issues/483.

This doesn't seem to be a memory leak IMO, so I will close this for now. If you have any further evidence this being a memory leak (memory never falling back down), we can always reopen this and have some further investigation :+1: