Skip to content

memory leak when using fopen with a stream wrapper #8548

Closed
@manueldimmler

Description

@manueldimmler

Description

The following code takes up a lot of memory and keeps increasing in a endless loop:

#!/usr/bin/php
<?php

class Wrapper
{

    public function stream_open(string $path, string $mode, int $options): bool
    {
        return true;
    }

}

$filename = tempnam(__DIR__, '');
touch($filename);

$wrapper = new Wrapper();
$counter = 0;
while ($counter < 1_000_000) {
    $counter++;
    stream_wrapper_register('symfony', Wrapper::class);
    fopen('symfony://' . $filename, 'r');
    stream_wrapper_unregister('symfony');
}

memory_leak_01

With a small change, the problem disappears:

#!/usr/bin/php
<?php

class Wrapper
{

    public function stream_open(string $path, string $mode, int $options): bool
    {
        return true;
    }

}

$filename = tempnam(__DIR__, '');
touch($filename);

$wrapper = new Wrapper();
$counter = 0;
stream_wrapper_register('symfony', Wrapper::class);
while ($counter < 1_000_000) {
    $counter++;
    fopen('symfony://' . $filename, 'r');
}
stream_wrapper_unregister('symfony');

memory_leak_02

The memory leak was described in the following ticket: symfony/symfony#46256

PHP Version

PHP 8.1.5 (cli)

Operating System

Ubuntu 20.04

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions