SimplePie Memory Leak Update
It seems quite a few people are still having trouble with SimplePie’s memory leaks, so I thought I would write a new post to say how I have modified SimplePie. If you aren’t a PHP programmer, you probably don’t want to read this post… For more background on this post, you probably want to read my other post.
It is possible that version 1.1 of SimplePie has fixed this issue, though Aman left a comment on my other post telling me that it didn’t. I currently use a heavily patched version of revision 901 from the SVN (I think… I may have updated since then…) so I can’t really tell you… I wish I had time to update and re-patch everything!
To fix the memory leak issues I was experiencing, I replaced the SimplePie::__destruct() method with a more "hardcore" version, hopefully forcing PHP to clear all references, and thus allowing it to clear memory:
<?php
function __destruct(){
foreach ($this->data as $k=>&$v) {
if (is_array($this->data[$k])) {
foreach($this->data[$k] as $l=>&$w) {
if (is_object($this->data[$k][$l])) $this->data[$k][$l]->__destruct();
unset($this->data[$k][$l]);
}
} else if (is_object($this->data[$k])) {
$this->data[$k]->__destruct();
}
unset($this->data[$k]);
}
unset($this->data);
$this->sanitize->__destruct();
unset($this->sanitize);
return true;
}
?>
Download this code: /code/32.phps
It seems to work for me, let me know if it works for you!

April 19th, 2008 at 11:10 pm
In SimplePie 1.1, you need to manually call the __destruct() method before unsetting your $feed variable. Is this an alteration of the function that 1.1 shipped with? Have you posted a bug to http://bugs.simplepie.org? If so, let me know, and we’ll integrate this patch.
August 5th, 2008 at 1:11 pm
I’m getting an error at:
$this->sanitize->__destruct;
…saying:
Message: Undefined property: SimplePie_Sanitize::$__destruct
How can I correct this? Thanks very much in advance for any info.
September 3rd, 2008 at 7:20 pm
Wow, I can’t believe they haven’t fixed the bug months after it was found! Thanks a bunch for your patch!
September 5th, 2008 at 8:02 am
Hi, this is Mike from the SimplePie project. This isn’t a bug in SimplePie, it’s a bug in PHP 5.2’s GC (garbage collector). Calling __destruct is just a way to compensate for this bug. The next version of PHP, version 5.3, which is slated to be released by October 2008, should fix this.
September 5th, 2008 at 9:13 am
Hi Michael, I realise this (now) – but it still needs a fix/workaround for people who want to parse large numbers of feeds at once until everyone is running 5.3 (which won’t be for a long time!). It’s annoying that you have to work around the limitations and bugs of your programming language, but that is life – and there are ways to do it without memory loss. Thanks for your clarification though!
September 5th, 2008 at 9:37 am
Vik, you need to put brackets after __destruct – i.e. __destruct(); – because it is a function not a value.
Hope this helps (probably not by now!), sorry for delay in replying – was on honeymoon when you wrote your comment, and have only just got internet at my new flat.
Benjie.
September 5th, 2008 at 10:42 pm
Hi Benjie! I was just trying to clear up Rémi Prévost’s misconception that this was a SimplePie bug.
I totally agree there needs to be a workaround for this until PHP 5.3 is released and calling the SimplePIe destruct method manually in the latest version of SimplePie (1.1.1) works partially to do that.
June 20th, 2009 at 12:56 am
Hi Benjie,
Thanks for your reply. The missing brackets are in your sample code — could you update it? Also, even if I add them, ie. __destruct(); — I still get this error:
Call to undefined method SimplePie_Sanitize::__destruct()
How can I correct this? Thanks very much in advance for this great, much-needed fix to SimplePie!
June 20th, 2009 at 9:23 am
Hi Vik,
I’ve fixed the brackets. Its possible that this ‘fix’ is no longer necessary – it is quite old now! I don’t remember, it could be one of two things:
1) There used to be a __destruct() for SimplePie_Sanitize that isn’t there any more (in which case delete that line from the PHP), or
2) You need to implement __destruct() for SP_S (probably using similar code to the above)
Sorry I can’t look into it more, I don’t use SimplePie any more as I have no need of PHP RSS readers. Let me know how you get on!
Benjie
June 20th, 2009 at 5:26 pm
Thanks very much for this info, Benjie!