-->

为什么会出现内存泄漏使用了SimplePie $用品 - > get_permalink时()

2019-08-06 22:43发布

我使用了SimplePie与PHP 5.3(启用GC)来分析我的RSS源。 这种运作良好,并没有做类似下面的问题时:

$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("Y-m-d H:i:s");
    $item->get_id();
    $item->get_title();
    $item->get_content();
    $item->get_description();
    $item->get_category();
}

内存调试超过100次迭代(使用不同的 RSS源):

但是,当使用$item->get_permalink()我的记忆调试看起来像这样超过100次迭代(使用不同的 RSS源)。

代码产生的问题

foreach ($simplePie->get_items() as $key => $item) {
    $item->get_date("Y-m-d 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();
}

事情我已经尝试

  • 使用get_link代替get_permalink
  • 使用__destroy提到这里 (即使它应该是固定的5.3)

当前的调试过程

我似乎已经查明问题到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截至目前。

我能做些什么来解决这个问题?

Answer 1:

其实,这是不是内存泄漏,但没有被清洗,而静态函数缓存!

这是由于SimplePie_IRI::set_iri (和set_authorityset_path )。 他们设置静态$cache变量,但他们没有设置或者为清洁本的新实例,当SimplePie被创建,这意味着仅变量变得越来越大。

这可以通过改变固定

public function set_authority($authority)
{
    static $cache;

    if (!$cache)
        $cache = array();

    /* etc */

public function set_authority($authority, $clear_cache = false)
{
    static $cache;
    if ($clear_cache) {
        $cache = null;
        return;
    }

    if (!$cache)
        $cache = array();

    /* etc */

在..等以下功能:

  • set_iri
  • set_authority
  • set_path

并增加了析构函数SimplePie_IRI使用静态缓存调用所有功能,用的参数true在$ clear_cache,将工作:

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

现在将导致内存消耗不随时间增加:

Git的问题



文章来源: Why am I getting memory leaks in SimplePie when using $item->get_permalink()?