SimplePie 1.5 is now available!

SimplePie Documentation.  Learn how to use this thing.  It's way better than going to school.

You are here: Documentation » Frequently Asked Questions » I'm getting memory leaks!

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.

In SimplePie 1.1 and above, 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()); 
}
?>

Other Notes

  • SimplePie 1.2 has made many improvements in memory handling, so make sure you're running SimplePie 1.2 or newer to improve memory consumption.
  • This PHP bug has been fixed in PHP 5.3. Upgrade if you can.
  • Retrieving feeds from the cache uses less memory than pulling them down and parsing them in the first place.
  • Pull fewer feeds all at once. This can be accomplished with a cron job and careful planning. Pull a smaller set of feeds first so that they can cache. Then pull another set. Then another. Let your pages pull directly from the cache, which uses less memory.
  • If you have control over your php.ini file, allocate more system memory to PHP. This will give you more buffer.
  • Many shared hosting plans have limited memory available to PHP per user anyway. You can't expect to parse 1000 feeds with 8 MB of RAM allocated. Take this into account. Perhaps a VPS would be better, or perhaps a cloud computing service like Amazon EC2 or Rackspace Cloud Servers.

See Also


faq/i_m_getting_memory_leaks.txt · Last modified: 2011/03/06 03:56 (external edit)