内存泄漏原则

时间:2018-02-13 11:25:48

标签: symfony memory-leaks doctrine batch-processing

我尝试迭代100K用户并且我的命令脚本中有内存泄漏我试着清除内存但它不起作用

所以:

    gc_enable();

    $categories = $this->categoryRep->findAll();

    foreach($categories as $category){

        $nbUsers    = $this->userRep->countUserCategoryNotLinked();
        $pages      = ceil($nbUsers/self::BATCH_SIZE);
        $output->writeln($nbUsers.' users without category links');

        $progress              = new ProgressBar($output, $nbUsers);
        $progress->start();

        for ($i = 0; $i < $pages; $i++) {

            $users = $this->userRep->getUserCategoryNotLinked(($i*self::BATCH_SIZE) , ($i+1)*self::BATCH_SIZE);

            /** @var User $user */
            foreach ($users as $user) {

                if ($user->getCodeCategory() == $category->getCodeCategory()) {
                    $user->setCategory($this->categoryRep->find($category->getId()));
                    $this->em->persist($user);
                }

                $user = null;
            }

            $progress->advance(self::BATCH_SIZE);
            $event = $stopwatch->lap('link');
            $output->writeln(' links added ... | - Memory : ' .number_format($event->getMemory() / 1048576, 2) . ' MB - Time : ' . number_format($event->getDuration() / 1000, 2) .' seconds');

            $this->em->flush();
            $this->em->clear();
            $users = null;
            gc_collect_cycles();
        }
    }

    $progress->finish();

当我在这里获得分页用户时内存泄漏:

$users = $this->userRep->getUserCategoryNotLinked(($i*self::BATCH_SIZE) , ($i+1)*self::BATCH_SIZE);

这是我的查询分页:

public function getUserCategoryNotLinked($offset,$limit)
    {
        $qb = $this->createQueryBuilder('u')
            ->where('u.category IS NULL')
            ->setFirstResult($offset)
            ->setMaxResults($limit);
        return $qb->getQuery()->getResult();
    }

我不明白我试图分离清除unset = null,但是当我得到用户时它仍然是内存泄漏:(

如果有人有想法?

1 个答案:

答案 0 :(得分:0)

我在这里犯了一个错误:

 $users = $this->userRep->getUserCategoryNotLinked(($i*self::BATCH_SIZE) , ($i+1)*self::BATCH_SIZE);

因为分页与学说不一样,所以它不是偏移量,而是页面就像这样

$users = $this->userRep->getUserCategoryNotLinked($i , self::BATCH_SIZE);