Bug #74072 [NEW]: Memory leaks with setter causing out of memory

From: Date: Fri, 10 Feb 2017 10:39:05 +0000
Subject: Bug #74072 [NEW]: Memory leaks with setter causing out of memory
Groups: php.bugs 
Request: Send a blank email to php-bugs+get-207244@lists.php.net to get a copy of this message
From:             ealexs at gmail dot com
Operating system: Debian 8
PHP version:      7.0.15
Package:          *General Issues
Bug Type:         Bug
Bug description:Memory leaks with setter causing out of memory

Description:
------------
We have a framework with an ORM within. The MODEL object has a setter in
it that causes the problems.
We pull ~100.000 records from a DB (mysqli) in batches of 1000.
I use "memory_get_usage(true)" to monitor the memory usage 
after each batch. 
The behaiviour is random. 
After the first batches memory_get_usage(true) = 2 MB. 
Then it starts to grow until it runs out of memory (set at 32MB atm).
In rare cases (refreshing) it finishes. 
It always does the same thing! (no data changed - devel DB).

If the setter (public function __set) is removed, or we avoid beeing
called,
then it always runs and memory_get_usage(true) stays at 2 MB. Always.

<?php
	public function __set($property, $value)
	{
		return $this->set($property, $value);
	}

	public function set($property, $value = null, $transform_with = null,
$requester = null, $reason = null)
	{
		if ($property{0} === "_")
		{
			$this->$property = $value;
			return;
		}
		// manage saving the old property
		if ((!(isset($this->_ols[$property]) || ($this->_ols &&
array_key_exists($property, $this->_ols)))) && (($value === null) ||
($this->$property !== $value)))
			$this->_ols[$property] = $this->$property;
		// end old
		$set = $this->getModelType()->properties[$property]->setter;
		if ($set)
			$this->$set($value);
		else
			$this->$property = $value;
	}
 
?>

I was unable to isolate the bug outside our framework atm.

Here is Valgrind's output (if the setter is removed or not called these
elements vanishes)

==6720== 
==6720== 2,299,968 bytes in 7,986 blocks are possibly lost in loss
record 9,815 of 9,817
==6720==    at 0x4C28C20: malloc (vg_replace_malloc.c:296)
==6720==    by 0x831CD8: __zend_malloc (zend_alloc.c:2864)
==6720==    by 0x863DF6: zend_hash_real_init_ex (zend_hash.c:140)
==6720==    by 0x863DF6: zend_hash_check_init (zend_hash.c:163)
==6720==    by 0x863DF6: _zend_hash_add_or_update_i (zend_hash.c:563)
==6720==    by 0x863DF6: _zend_hash_add (zend_hash.c:638)
==6720==    by 0x88D53C: zend_hash_add_mem (zend_hash.h:570)
==6720==    by 0x88D53C: zend_get_property_guard
(zend_object_handlers.c:503)
==6720==    by 0x88FB76: zend_std_write_property
(zend_object_handlers.c:667)
==6720==    by 0x8B71E3: zend_assign_to_object (zend_execute.c:1234)
==6720==    by 0x8B71E3: ZEND_ASSIGN_OBJ_SPEC_CV_CONST_HANDLER
(zend_vm_execute.h:32028)
==6720==    by 0x89584A: execute_ex (zend_vm_execute.h:414)
==6720==    by 0x8E9566: zend_execute (zend_vm_execute.h:458)
==6720==    by 0x857EF3: zend_execute_scripts (zend.c:1437)
==6720==    by 0x7FB23F: php_execute_script (main.c:2492)
==6720==    by 0x8EB1F9: do_cli (php_cli.c:977)
==6720==    by 0x449796: main (php_cli.c:1347)
==6720== 

==6720== 
==6720== 18,452,736 bytes in 8,009 blocks are possibly lost in loss
record 9,817 of 9,817
==6720==    at 0x4C28C20: malloc (vg_replace_malloc.c:296)
==6720==    by 0x831CD8: __zend_malloc (zend_alloc.c:2864)
==6720==    by 0x862C3F: zend_hash_real_init_ex (zend_hash.c:140)
==6720==    by 0x862C3F: zend_hash_real_init (zend_hash.c:205)
==6720==    by 0x88E192: rebuild_object_properties
(zend_object_handlers.c:82)
==6720==    by 0x8900B2: zend_std_write_property
(zend_object_handlers.c:709)
==6720==    by 0x8BCAF2: zend_assign_to_object (zend_execute.c:1234)
==6720==    by 0x8BCAF2: ZEND_ASSIGN_OBJ_SPEC_UNUSED_CV_HANDLER
(zend_vm_execute.h:26390)
==6720==    by 0x89584A: execute_ex (zend_vm_execute.h:414)
==6720==    by 0x849650: zend_call_function (zend_execute_API.c:858)
==6720==    by 0x874A32: zend_call_method (zend_interfaces.c:104)
==6720==    by 0x88FBD9: zend_std_call_setter
(zend_object_handlers.c:216)
==6720==    by 0x88FBD9: zend_std_write_property
(zend_object_handlers.c:674)
==6720==    by 0x8B71E3: zend_assign_to_object (zend_execute.c:1234)
==6720==    by 0x8B71E3: ZEND_ASSIGN_OBJ_SPEC_CV_CONST_HANDLER
(zend_vm_execute.h:32028)
==6720==    by 0x89584A: execute_ex (zend_vm_execute.h:414)
==6720==    by 0x8E9566: zend_execute (zend_vm_execute.h:458)
==6720==    by 0x857EF3: zend_execute_scripts (zend.c:1437)
==6720==    by 0x7FB23F: php_execute_script (main.c:2492)
==6720==    by 0x8EB1F9: do_cli (php_cli.c:977)
==6720==    by 0x449796: main (php_cli.c:1347)
==6720== 




-- 
Edit bug report at https://bugs.php.net/bug.php?id=74072&edit=1
-- 
Try a snapshot (PHP 5.4):   https://bugs.php.net/fix.php?id=74072&r=trysnapshot54
Try a snapshot (PHP 5.5):   https://bugs.php.net/fix.php?id=74072&r=trysnapshot55
Try a snapshot (trunk):     https://bugs.php.net/fix.php?id=74072&r=trysnapshottrunk
Fixed in SVN:               https://bugs.php.net/fix.php?id=74072&r=fixed
Fixed in release:           https://bugs.php.net/fix.php?id=74072&r=alreadyfixed
Need backtrace:             https://bugs.php.net/fix.php?id=74072&r=needtrace
Need Reproduce Script:      https://bugs.php.net/fix.php?id=74072&r=needscript
Try newer version:          https://bugs.php.net/fix.php?id=74072&r=oldversion
Not developer issue:        https://bugs.php.net/fix.php?id=74072&r=support
Expected behavior:          https://bugs.php.net/fix.php?id=74072&r=notwrong
Not enough info:            https://bugs.php.net/fix.php?id=74072&r=notenoughinfo
Submitted twice:            https://bugs.php.net/fix.php?id=74072&r=submittedtwice
register_globals:           https://bugs.php.net/fix.php?id=74072&r=globals
PHP 4 support discontinued: https://bugs.php.net/fix.php?id=74072&r=php4
Daylight Savings:           https://bugs.php.net/fix.php?id=74072&r=dst
IIS Stability:              https://bugs.php.net/fix.php?id=74072&r=isapi
Install GNU Sed:            https://bugs.php.net/fix.php?id=74072&r=gnused
Floating point limitations: https://bugs.php.net/fix.php?id=74072&r=float
No Zend Extensions:         https://bugs.php.net/fix.php?id=74072&r=nozend
MySQL Configuration Error:  https://bugs.php.net/fix.php?id=74072&r=mysqlcfg



Thread (8 messages)

« previous php.bugs (#207244) next »