Skip to content

OCI_RETURN_LOBS flag causes oci8 to leak memory #18873

Open
@mvorisek

Description

@mvorisek

Description

The following code:

$expectedStr = str_repeat('a', 1_001);
$sql = 'select concat(TO_CLOB(\'' . str_repeat('a', 1_000) . '\'), TO_CLOB(\'a\')) AS "v" from "DUAL"';

$memUsages = array_flip(range(0, 100 - 1));
foreach (array_keys($memUsages) as $k) {
    $stid = oci_parse($c, $sql);
    oci_execute($stid);
    $row = oci_fetch_array($stid, \OCI_ASSOC | \OCI_RETURN_LOBS);
    $res = $row['v'];
    if ($res !== $expectedStr) {
        var_dump([$expectedStr, $res]);
        throw new \Exception('unexpected result');
    }

    $memUsages[$k] = memory_get_usage();
}

$memUsages = array_slice($memUsages, 1, null, true);
$memUsages = array_unique($memUsages);

if (count($memUsages) !== 1) {
    var_dump($memUsages);
    throw new \Exception('memory leak detected');
}

echo "ok\n";

https://raw.githubusercontent.com/mvorisek/pecl-database-oci8/9a1241341cd02aa68c587046f11b0343bc713b8f/tests/gh-4-return-lob.phpt (2 lines changed of https://raw.githubusercontent.com/php/pecl-database-oci8/9a1241341cd02aa68c587046f11b0343bc713b8f/tests/gh-4.phpt already present test)

Resulted in this output:

array(9) {
  [1]=>
  int(6872016)
  [2]=>
  int(6872368)
  [3]=>
  int(6872400)
  [4]=>
  int(6872432)
  [5]=>
  int(6872464)
  ...
}
PHP Fatal error:  Uncaught Exception: memory leak detected in ...

But I expected this output instead:

ok

Notes:

php/pecl-database-oci8#29 fixed memory leak once after 8th descriptor create, and such fix can be backported into PHP 8.3 however it is not critical and maybe it can be even considered a feature.

This issue is about a clear memory leak after every iteration when the data are fetched using OCI_RETURN_LOBS flag. Such flag imply the data are not returned as descriptor, but as a string instead. So it seems the descriptor is somehow not released after the data are read.

PHP Version

PHP 8.2 - master

Operating System

any (tested linux and Windows)

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions