You are here: Documentation » Frequently Asked Questions » I'm getting memory leaks!
This is an old revision of the document!
I'm getting memory leaks!
When processing a large number of feeds, a memory leak can occur causing PHP to run out of memory. This is due to PHP Bug #33595 where PHP doesn't release memory when making recursive (i.e. self-referential) object calls. The problem is due to recursive references within SimplePie (and PHP's poor handling of said references). This issue affects all versions of PHP earlier than 5.3 (which has not yet been released).
Solution for 1.1
You simply need to call the destructor method before unsetting the $feed
and “$item” variables.
<?php for ($i = 1; $i < 10; $i++) { $feed = new SimplePie(); $feed->set_feed_url($url); $feed->init(); $feed->handle_content_type(); $item = $feed->get_item(0); $feed->__destruct(); // Do what PHP should be doing on it's own. unset($item); unset($feed); echo "Memory usage: " . number_format(memory_get_usage()); } ?>
Possible Solution for 1.0.1 (not officially supported)
A solution that works for me is patching vanilla SimplePie 1.0.1 with this:
--- simplepie1/simplepie.inc (revision 528) +++ simplepie1/simplepie.inc (working copy) @@ -668,6 +668,12 @@ $this->init(); } } + function __destruct() { + if (isset($this->data['items']) && is_array($this->data['items'])) foreach (array_keys($this->data['items']) as $k) { + $this->data['items'][$k]->__destruct(); + unset($this->data['items'][$k]); + } + } /** * Used for converting object to a string @@ -1521,6 +1527,7 @@ return false; } } + $locate->__destruct(); $locate = null; $headers = $file->headers; @@ -2703,6 +2710,9 @@ $this->feed = $feed; $this->data = $data; } + function __destruct() { + unset($this->feed); + } function __toString() { @@ -10013,6 +10023,9 @@ $this->timeout = $timeout; $this->max_checked_feeds = $max_checked_feeds; } + function __destruct() { + unset($this->file); + } function find($type = SIMPLEPIE_LOCATOR_ALL) {
Then all you have to do in your code is ensure that you call $sp->__destruct()
once you are done with the SimplePie instance.
If you don't need multiple concurrent SimplePie classes (i.e. you use SimplePie in a serial fashion) you could use a custom loader to do that… for example:
function SimplePie_Loader($url) { static $sp = NULL; if ($sp !== NULL) { $sp->__destruct(); $sp = NULL; } $sp = new SimplePie($url); return $sp; }
See Also
faq/i_m_getting_memory_leaks.1239446926.txt.gz · Last modified: 2011/03/06 03:56 (external edit)