php.net |  support |  documentation |  report a bug |  advanced search |  search howto |  statistics |  random bug |  login
Bug #46790 Memory leak with classes
Submitted: 2008-12-07 06:34 UTC Modified: 2008-12-07 15:39 UTC
From: o_O_Tync at mail dot ru Assigned:
Status: Not a bug Package: Class/Object related
PHP Version: 5.2.7 OS: Win, FreeBSD
Private report: No CVE-ID: None
 [2008-12-07 06:34 UTC] o_O_Tync at mail dot ru
Description:
------------
I've come across a memory leak: garbage collector failes to free memory occupied by linked objects, such as:

$obj1->pair = $obj2; $obj2->pair = $obj1;

Below goes the problematic piece of code: over many iterations - it occupies a lot of memory.

If we comment 'line1' or 'line2' or both - memory is okay (48 Kb), but cross-linking reveals a bug (839 Kb) despite of explicit unsetting.
The commented 'this helps' line solves the problem.

Tested under WinXP, and FreeBSD 6.1 using the latest PHP 5.2.7

Reproduce code:
---------------
<?php
class Paired
	{
	/** Paired object
	 * @var Paired
	 */
	public $pair;
	}

for ($i=0;$i<2000;$i++)
	{
	$obj1 = new Paired; $obj2 = new Paired;
	$obj1->pair = $obj2; // 'line1'
	$obj2->pair = $obj1; // 'line2'
	// $obj1->pair = null; $obj2->pair = null; // this helps
	unset($obj1); unset($obj2); // Explicitly
	}

print 'Mem: '.floor(memory_get_usage()/1024).' Kb';
?>

Expected result:
----------------
Mem: 48 Kb

Actual result:
--------------
Mem: 839 Kb

Patches

Pull Requests

History

AllCommentsChangesGit/SVN commitsRelated reports
 [2008-12-07 07:13 UTC] lstrojny@php.net
Please try using this CVS snapshot:

  http://snaps.php.net/php5.3-latest.tar.gz
 
For Windows:

  http://windows.php.net/snapshots/


 [2008-12-07 13:18 UTC] o_O_Tync at mail dot ru
>php.exe -v
PHP 5.3.0alpha4-dev (cli) (built: Dec  6 2008 17:07:05)
Copyright (c) 1997-2008 The PHP Group
Zend Engine v2.3.0, Copyright (c) 1998-2008 Zend Technologies

Without the 'this helps' line
>php.exe -f memleak.php
Mem: 1166 Kb

With the 'this helps' line
>php.exe -f memleak.php
Mem: 321 Kb

The problem remains
 [2008-12-07 15:33 UTC] scottmac@php.net
Sorry, but your problem does not imply a bug in PHP itself.  For a
list of more appropriate places to ask for help using PHP, please
visit http://www.php.net/support.php as this bug system is not the
appropriate forum for asking support questions.  Due to the volume
of reports we can not explain in detail here why your report is not
a bug.  The support channels will be able to provide an explanation
for you.

Thank you for your interest in PHP.

You have a cyclic reference here, even when you unset $obj1 and $obj2 there are still references to each other that exist.

However PHP 5.3 has a garbage collector which will correctly deal with this.
 [2008-12-07 15:39 UTC] scottmac@php.net
I should also add that you can check your phpinfo() to see if zend.enable_gc is enabled, if not then add it to php.ini

You then only need to call gc_collect_cycles() to run the garbage collector, it also runs automatically at certain times.
 
PHP Copyright © 2001-2025 The PHP Group
All rights reserved.
Last updated: Sat Jul 19 01:00:03 2025 UTC