内存调试symfony2

时间:2014-03-28 11:57:40

标签: symfony orm doctrine

我正在使用doctrine在sql中插入批量数据。但是在插入600到700条记录后,它会出错 致命错误:允许的内存大小为33554432字节(尝试分配24个字节)

我尝试增加php内存限制,但没有用。

这是我的代码:

function DB_InsertPostTag($ postID,$ listSize,$ tagList,$ tagTypeID,$ categoryid) {

 $returnVal = 0;

 $insertList = array(1=>'');

 $insertCount = 0;
         sort($tagList);
         $tagCount = 0;
      for ($i = 0; $i <$listSize; $i++)
      {
           $tagCount++;
           if(isset($tagList[$i+1]))
      {

        if ($tagList[$i] != $tagList[$i+1])
           {
                $insertCount++;
                $tagList[$i];
                $tagCount = 0; //reset the counter

           }

       }
           //else we just ramp the counter
      }
      $batchSize = 1;
     for ($i = 1; $i < $insertCount; $i++)
      {

           $post_tag = new PostTag();
           $post_tag->setPostId($postID);
           $post_tag->setCategoryId($categoryid);
           $post_tag->setTagId($tagList[$i]);
           $post_tag->setTagTypeId($tagTypeID);
           $post_tag->setTagCount($tagCount);
           $em1 = $this->em;
           $em1->persist($post_tag);
            $id = $post_tag->getId();
           if (($i % $batchSize) == 0) {
            $em1->flush();
            $em1->clear(); // Detaches all objects from Doctrine!
        }
        }
        $em1->flush();
        $em1->clear();
      $returnVal = $id;

 return $returnVal;

}

1 个答案:

答案 0 :(得分:0)

如果您使用DQL,请尝试在一定数量的行之后进行刷新,这样php就可以从内存中清除这些对象。