# Memory Leaks with Objects (no circular reference)

- Thread: https://externals.io/message/43213
- Original archive: https://news-web.php.net/php.internals/43213
- Started by Igor Feghali on 2009-02-27T18:25:17+00:00
- Messages: 4
- Reply by email to internals@lists.php.net
- More for agents: search with https://externals.io/search.md?q={query} · JSON API at https://externals.io/api · documentation at https://externals.io/llms.txt

---

## #43213 — Igor Feghali — 2009-02-27T18:25:17+00:00

URL: https://externals.io/message/43213#43213

Hi,

I am facing some memory leaks with php 5.1.6 that has been fixed in
5.2.0. So I suppose the fix should be somewhere between those
versions, but I couldn't find exactly which line in the NEWS file is
that.

<?php

class foo
{
   var $a1;
   var $a2;
   var $a3;
   var $a4;
   var $a5;
   var $a6;
   var $a7;
   var $a8;
   var $a9;
}

class bar
{
   var $a1;
   var $a2;
   var $a3;
   var $a4;
   var $a5;
   var $a6;
   var $a7;
   var $a8;
}

echo "--- 9 ---\n";
for ($i = 0; $i < 5; $i++){
   $u =& new foo();
   unset($u);
   echo `memory_get_usage()`."\n";
}

echo "--- 8 ---\n";
for ($i = 0; $i < 5; $i++){
   $u =& new bar();
   unset($u);
   echo `memory_get_usage()`."\n";
}

?>

Output for 5.1.6:
--- 9 ---
44712
44784
44848
44912
44976
--- 8 ---
45008
45008
45008
45008
45008

Conclusion: memory consumption keeps constant for the class with 8
properties. unset() doesnt frees all the memory up for the class with
9 properties.

Output for 5.2.0:
--- 9 ---
66784
66828
66828
66828
66828
--- 8 ---
66896
66896
66896
66896
66896

Conclusion: memory consumption constant along the whole code, as expected.

I have seen that reported many times in the bugs system, but in all
the cases it ended up being something related to circular references
which is not the case here. Some people also reported that replacing
"unset($obj)" with "$obj = null" would help but I tried it and the
problem remains.

Any help on how to workaround that in userland would be very
appreciated. Surely upgrading will fix that, but I am trying to keep
compatibility with prior versions as much as possible.

Best,
~IF.

---

## #43266 — Dmitry Stogov — 2009-03-06T07:16:38+00:00

URL: https://externals.io/message/43213#43266 · In reply to: #43213

Hi Igor,

It's definitely not a memory leak. The memory manager in php-5.2 was 
changed so it behaves little bit different. BTW: both 5.1 and 5.2 use 
some caches of the chunks with most useful sizes. They allow to 
eliminate annecessury free()/malloc() calls and speedup the whole 
system. `memory_get_usage()` returns allocated memory including these 
caches, so you can think that memory lost however it's just preallocated 
and ready for next emalloc()s.

Thanks. Dmitry.

Igor Feghali wrote:
> Hi,
> 
> I am facing some memory leaks with php 5.1.6 that has been fixed in
> 5.2.0. So I suppose the fix should be somewhere between those
> versions, but I couldn't find exactly which line in the NEWS file is
> that.
> 
> <?php
> 
> class foo
> {
>    var $a1;
>    var $a2;
>    var $a3;
>    var $a4;
>    var $a5;
>    var $a6;
>    var $a7;
>    var $a8;
>    var $a9;
> }
> 
> class bar
> {
>    var $a1;
>    var $a2;
>    var $a3;
>    var $a4;
>    var $a5;
>    var $a6;
>    var $a7;
>    var $a8;
> }
> 
> echo "--- 9 ---\n";
> for ($i = 0; $i < 5; $i++){
>    $u =& new foo();
>    unset($u);
>    echo `memory_get_usage()`."\n";
> }
> 
> echo "--- 8 ---\n";
> for ($i = 0; $i < 5; $i++){
>    $u =& new bar();
>    unset($u);
>    echo `memory_get_usage()`."\n";
> }
> 
> ?>
> 
> Output for 5.1.6:
> --- 9 ---
> 44712
> 44784
> 44848
> 44912
> 44976
> --- 8 ---
> 45008
> 45008
> 45008
> 45008
> 45008
> 
> Conclusion: memory consumption keeps constant for the class with 8
> properties. unset() doesnt frees all the memory up for the class with
> 9 properties.
> 
> Output for 5.2.0:
> --- 9 ---
> 66784
> 66828
> 66828
> 66828
> 66828
> --- 8 ---
> 66896
> 66896
> 66896
> 66896
> 66896
> 
> Conclusion: memory consumption constant along the whole code, as expected.
> 
> I have seen that reported many times in the bugs system, but in all
> the cases it ended up being something related to circular references
> which is not the case here. Some people also reported that replacing
> "unset($obj)" with "$obj = null" would help but I tried it and the
> problem remains.
> 
> Any help on how to workaround that in userland would be very
> appreciated. Surely upgrading will fix that, but I am trying to keep
> compatibility with prior versions as much as possible.
> 
> Best,
> ~IF.

---

## #43271 — Igor Feghali — 2009-03-06T19:44:55+00:00

URL: https://externals.io/message/43213#43271 · In reply to: #43266

Hello Dmitry,

Thank you for your answer. The problem comes when you put that piece
of code inside a Daemon that is supposed to run 24h/7 days. The
reserved memory increases at each loop, never gets freed, and in about
1~2 days the process dies because it reaches the memory limit of
128MB.

Isn't there any way to force PHP to free that space up ?

Regards,
~IF.

---

## #43280 — Dmitry Stogov — 2009-03-09T10:31:24+00:00

URL: https://externals.io/message/43213#43280 · In reply to: #43271

Hi,

these caches can't grow over some internal limits and they can't take 
128M. It may be some other source of the problem. Could you create a 
script which reproduces memory overflow?

Thanks. Dmitry.

Igor Feghali wrote:
> Hello Dmitry,
> 
> Thank you for your answer. The problem comes when you put that piece
> of code inside a Daemon that is supposed to run 24h/7 days. The
> reserved memory increases at each loop, never gets freed, and in about
> 1~2 days the process dies because it reaches the memory limit of
> 128MB.
> 
> Isn't there any way to force PHP to free that space up ?
> 
> Regards,
> ~IF.
