|
php.net | support | documentation | report a bug | advanced search | search howto | statistics | random bug | login |
[2013-07-25 21:38 UTC] vitalif at mail dot ru
Description:
------------
Hello!
I've discovered that when setting properties via __set() the object takes much more memory than it should. It's reproducible at least on PHP 5.5 and 5.4. Is it a memory leak?
Test script:
---------------
<?php
// Memory leak somewhere around __set?
class A
{
var $data = array();
function __get($k)
{
return $this->data[$k];
}
function __set($k, $v)
{
return $this->data[$k] = $v;
}
}
$b = new A();
for ($i = 0; $i < 500000; $i++)
$b->{"a$i"} = 'abc';
var_dump(memory_get_usage()); // int(78318488) - why so big?
$c = clone $b;
unset($b);
var_dump(memory_get_usage()); // int(42220972) - OK
unset($c);
$b = new A();
for ($i = 0; $i < 500000; $i++)
$b->__set("a$i", 'abc');
var_dump(memory_get_usage()); // int(42221492) - OK
Expected result:
----------------
I expect roughly the same memory usage at all three points. Like:
int(42220972)
int(42220972)
int(42221492)
Actual result:
--------------
The first value is much bigger:
int(78318488)
int(42220972)
int(42221492)
PatchesPull RequestsHistoryAllCommentsChangesGit/SVN commits
|
|||||||||||||||||||||||||||||||||||||
Copyright © 2001-2025 The PHP GroupAll rights reserved. |
Last updated: Fri Nov 07 03:00:01 2025 UTC |
@ealexs: A magic accessor should not trigger a magic accessor on a different property name on the same object. $a->__get('x') -> $a->__set('x') is fine. $a->__get('x') -> $b->__get('y') is fine. $a->__get('x') -> $a->__get('y') may still lead to unbounded memory growth, if this is done for many distinct property names.