about:benjie

Random learnings and other thoughts from an unashamed geek

SimplePie Memory Leak Update

| Comments

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:

(32.phps) download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
<?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;
}
?>

It seems to work for me, let me know if it works for you!

Comments