Why am I getting memory leaks in SimplePie when using $ item-> get_permalink ()?

I am using SimplePie with PHP 5.3 (with gc enabled) to parse my RSS feeds. This works well and without problems when you do something like the following:

$simplePie = new SimplePie(); $simplePie->set_feed_url($rssURL); $simplePie->enable_cache(false); $simplePie->set_max_checked_feeds(10); $simplePie->set_item_limit(0); $simplePie->init(); $simplePie->handle_content_type(); foreach ($simplePie->get_items() as $key => $item) { $item->get_date("Ymd H:i:s"); $item->get_id(); $item->get_title(); $item->get_content(); $item->get_description(); $item->get_category(); } 

Debugging memory over 100 iterations (using different RSS feeds):

SimplePie without using get_permalink ()

But when using $item->get_permalink() my memory debugging looks like about 100 iterations (using different RSS feeds).

Code to create the problem :

 foreach ($simplePie->get_items() as $key => $item) { $item->get_date("Ymd H:i:s"); $item->get_id(); $item->get_title(); $item->get_permalink(); //This creates a memory leak $item->get_content(); $item->get_description(); $item->get_category(); } 

SimplePie get_permalink memory leak

What I tried :

  • Using get_link instead of get_permalink
  • Using __destroy as mentioned here (although it should be fixed for 5.3)

Current debugging process :

I seem to have traced this issue to SimplePie_Item::get_permalink โ†’ SimplePie_Item::get_link โ†’ SimplePie_Item::get_links โ†’ SimplePie_Item::sanitize โ†’ SimplePie::sanitize โ†’ SimplePie_Sanitize::sanitize โ†’ SimplePie_Registry::call โ†’ SimplePie_IRI::absolutize .

What can I do to fix this?

+4
source share
1 answer

Actually, this is not a memory leak, but static caches that are not cleared!

This is due to SimplePie_IRI::set_iri (and set_authority and set_path ). They set the static $cache variable, but they will not cancel or clear it when creating a new SimplePie instance, which means the variables are getting bigger and bigger.

This can be fixed by changing

 public function set_authority($authority) { static $cache; if (!$cache) $cache = array(); /* etc */ 

to

 public function set_authority($authority, $clear_cache = false) { static $cache; if ($clear_cache) { $cache = null; return; } if (!$cache) $cache = array(); /* etc */ 

.. etc. in the following functions:

  • set_iri ,
  • set_authority ,
  • set_path ,

And adding a destructor to SimplePie_IRI that calls all functions using a static cache with the true parameter in $ clear_cache will work:

 /** * Clean up */ public function __destruct() { $this->set_iri(null, true); $this->set_path(null, true); $this->set_authority(null, true); } 

Which now will not lead to an increase in memory consumption over time:

Simplepie fixed

Git Problem

+9
source

All Articles