获取数据时Doctrine2内存泄漏

时间:2014-10-11 10:01:26

标签: php memory-leaks doctrine-orm

我需要使用Doctrine处理大量数据,但我在Doctrine2中面临内存泄漏,我无法弄清楚我还能做些什么来避免它。 我尽可能地简化了代码,但即使这个简单的代码也会导致内存泄漏(请注意我不做任何插入,更新,刷新等等。只选择数据)

    gc_enable();

    $conn = $em->getConnection();
    $conn->getConfiguration()->setSQLLogger(null); // disabling Logger

    $batchSize = 10000;

    for ($i=0; $i<$batchSize*10; $i+=$batchSize) {
        echo "#".$i." - ";

        $query = $em->createQuery('
            SELECT
                p
            FROM
                Application\Entity\Product p
            WHERE
                p.id > :id
            ORDER BY
                p.id ASC
        ')
            ->setParameter('id', $i)
            ->setMaxResults($batchSize);

        $query->useQueryCache(false);
        $query->useResultCache(false);

        $products = $query->getResult();

        // trying to clear all that is possible
        $query = null;
        $products = null;

        $em->clear();
        $em->close();

        gc_collect_cycles();

        $mu = memory_get_usage(true);
        echo "Memory: ".number_format($mu/1000000, 2)." Mb\n";
    }

这是输出:

#0 - Memory: 87.29 Mb
#10000 - Memory: 94.37 Mb
#20000 - Memory: 100.66 Mb
#30000 - Memory: 110.36 Mb
#40000 - Memory: 119.01 Mb
#50000 - Memory: 123.99 Mb
#60000 - Memory: 131.07 Mb
#70000 - PHP Fatal error:  Allowed memory size of 134217728 bytes exhausted (tried to allocate 40 bytes) in ...\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php on line 2615

我还可以尝试做些什么来摆脱这种泄漏?

Doctrine ORM 2.4.6 Doctrine DBAL 2.4.2

0 个答案:

没有答案