【问题标题】:Doctrine Batch Processing doesn't clear memory?Doctrine Batch Processing 不会清除内存?
【发布时间】:2013-07-08 00:58:54
【问题描述】:

我正在尝试使用a technique suggested by Doctrine 2 处理大量对象。该技术表明,通过使用迭代器并在处理每次迭代后进行分离,内存使用量应保持在最低限度(他们说处理 10000 条记录会增加几 KB)。

但是,当我尝试这样做时,我没有看到任何对象被释放。事实上,我正在检索超过 2000 个资产,这使我的内存使用量增加了 90 MB。显然,这些对象没有被释放。谁能告诉我我做错了什么?我的代码如下所示:

$profiles = //array of Profile entities 
$qb = $this->createQueryBuilder('a')
            ->addSelect('file')
            ->leftJoin($profileNodeEntityName, 'pn', JOIN::WITH, 'pn.icon = a OR pn.asset = a')
            ->leftJoin(
                $profileEntityName,
                'p',
                JOIN::WITH,
                'pn.profile = p OR p.logo = a OR p.background = a OR p.pricelistAsset = a OR p.pdfTplBlancAsset = a OR p.pdfTplFrontAsset = a OR p.pdfTplBackAsset = a'
            )
            ->innerJoin('a.currentFile', 'file')
            ->where('p IN (:profiles)')
            ->setParameter('profiles', $profiles)
            ->distinct(true);

        $iterableResult = $qb->getQuery()->iterate();

        $start = memory_get_usage() / 1024;
        while (($row = $iterableResult->next()) !== false) {
            // process $row[0]
            $this->getEntityManager()->detach($row[0]);
        }
        $end = memory_get_usage() / 1024 - $start;
        // $end is more of less equal to 90000 or 90 MB

谢谢!

【问题讨论】:

    标签: symfony doctrine batch-processing


    【解决方案1】:

    您还应该分离相关实体,或在关联上设置 cascade={"detach"}。

    【讨论】:

    • 是的,应该。我使用相同的技术将超过一百万个实体序列化为 XML,同时不使用超过 60 兆的内存。
    • 还有一件事:教义手册告诉 iterate() 方法不适用于具有连接实体的结果集。 '对于获取-加入集合值关联的查询,迭代结果是不可能的。此类 SQL 结果集的性质不适合增量水合。'
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-08-11
    • 2018-05-26
    • 1970-01-01
    • 2014-11-13
    • 1970-01-01
    • 2020-12-01
    相关资源
    最近更新 更多