php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #74575 Memory leaks with gc_collect_cycles()
Submitted: 2017-05-11 16:14 UTC Modified: 2017-06-25 17:06 UTC
From: roocster at gmail dot com Assigned:
Status: Not a bug Package: Scripting Engine problem
PHP Version: 7.1.5RC1 OS: *
Private report: No CVE-ID: None
 [2017-05-11 16:14 UTC] roocster at gmail dot com
Description:
------------
If make the situation described in the docs of "Collecting Cycles" (http://php.net/manual/en/features.gc.collecting-cycles.php) the gc_collect_cycles() doesn't collect garbage cycle reference.

After we made an array which is referenced only by itself element (cycle reference) the gc_collect_cycles() call returns 0. That means there are not collected cycles and we have a memory leak.

If we replace "$a = $a[0]" on "unset($a)" the garbage collector works correctly and the gc_collect_cycles() call returns 1.

Test script:
---------------
<?php
echo "Memory usage before memory leak:\t",
memory_get_usage()," bytes",PHP_EOL;

$a = ['one'];
$a[] = &$a;
$a = $a[0];

echo "Memory usage after memory leak:\t\t",
memory_get_usage()," bytes",PHP_EOL;

echo "Number of collected cycles: ",
gc_collect_cycles(),PHP_EOL;

echo "Memory usage after gc_collect_cycles():\t",
memory_get_usage()," bytes",PHP_EOL;

Actual result:
--------------
Memory usage before memory leak:        353368 bytes
Memory usage after memory leak:         353392 bytes
Number of collected cycles: 0
Memory usage after gc_collect_cycles(): 353392 bytes

Patches

Add a Patch

Pull Requests

Add a Pull Request

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2017-05-11 16:32 UTC] roocster at gmail dot com
-Operating System: Windows 10 x64 +Operating System: *
 [2017-05-11 16:32 UTC] roocster at gmail dot com
The bug is also reproduced on linux
 [2017-05-14 18:56 UTC] cmb@php.net
-Package: *General Issues +Package: Scripting Engine problem
 [2017-06-25 17:06 UTC] nikic@php.net
-Status: Open +Status: Not a bug
 [2017-06-25 17:06 UTC] nikic@php.net
$a is a reference in your example, so the $a = $a[0] assignment already breaks the cyclce. As such, gc_collect_cycles() has nothing to collect.
 
PHP Copyright © 2001-2024 The PHP Group
All rights reserved.
Last updated: Fri Apr 19 17:01:30 2024 UTC