我试图在MySQL中插入Doctrine2
的20000年记录 foreach ($getIds as $id){// 20000 Ids
$twNode = new TwNode();
$twNode->setTwId($id);
$twNode->addAcFriend($this->twAccount);
$this->twAccount->addFollower($twNode);
$this->em->persist($twNode);
$this->em->persist($this->twAccount);
$this->em->flush();
$this->output->write('W');
$followersWrite++;
}
}
然而,此错误happ,我如何避免此允许记忆错误?
PHP Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 72 bytes) in /Users/whitebear/httproot/myapp/vendor/monolog/monolog/src/Monolog/Formatter/NormalizerFormatter.php on line 62
PHP Stack trace:
PHP 1. {main}() /Users/whitebear/httproot/myapp/app/console:0
PHP 2. SymfonyComponentConsoleApplication->run() /Users/whitebear/httproot/myapp/app/console:22
PHP 3. SymfonyBundleFrameworkBundleConsoleApplication->doRun() /Users/whitebear/httproot/myapp/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:106
PHP 4. SymfonyComponentConsoleApplication->doRun() /Users/whitebear/httproot/myapp/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:83
PHP 5. SymfonyComponentConsoleCommandCommand->run() /Users/whitebear/httproot/myapp/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:200
PHP 6. AcmeTopBundleCommandtwCrawlerCommand->execute() /Users/whitebear/httproot/myapp/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:242
PHP 7. AcmeTopBundleCommandtwCrawlerCommand->initialRun() /Users/whitebear/httproot/myapp/src/Acme/TopBundle/Command/twCrawlerCommand.php:430
PHP 8. EntityManager52ee6dcbdb2bb_546a8d27f194334ee012bfe64f629947b07e4919__CG__DoctrineORMEntityManager->flush() /Users/whitebear/httproot/myapp/src/Acme/TopBundle/Command/twCrawlerCommand.php:284
PHP 9. DoctrineORMEntityManager->flush() /Users/whitebear/httproot/myapp/app/cache/dev/jms_diextra/doctrine/EntityManager_52ee6dcbdb2bb.php:402
PHP 10. DoctrineORMUnitOfWork->commit() /Users/whitebear/httproot/myapp/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:389
PHP 11. DoctrineORMUnitOfWork->executeInserts() /Users/whitebear/httproot/myapp/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:335
PHP 12. DoctrineORMPersistersBasicEntityPersister->executeInserts() /Users/whitebear/httproot/myapp/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:952
PHP 13. DoctrineDBALStatement->execute() /Users/whitebear/httproot/myapp/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:279
PHP 14. DoctrineDBALLoggingLoggerChain->startQuery() /Users/whitebear/httproot/myapp/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php:161
PHP 15. SymfonyBridgeDoctrineLoggerDbalLogger->startQuery() /Users/whitebear/httproot/myapp/vendor/doctrine/dbal/lib/Doctrine/DBAL/Logging/LoggerChain.php:54
PHP 16. SymfonyBridgeDoctrineLoggerDbalLogger->log() /Users/whitebear/httproot/myapp/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php:80
PHP 17. MonologLogger->debug() /Users/whitebear/httproot/myapp/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php:102
PHP 18. MonologLogger->addRecord() /Users/whitebear/httproot/myapp/vendor/monolog/monolog/src/Monolog/Logger.php:423
PHP 19. MonologHandlerAbstractProcessingHandler->handle() /Users/whitebear/httproot/myapp/vendor/monolog/monolog/src/Monolog/Logger.php:244
PHP 20. MonologFormatterLineFormatter->format() /Users/whitebear/httproot/myapp/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php:35
PHP 21. MonologFormatterNormalizerFormatter->format() /Users/whitebear/httproot/myapp/vendor/monolog/monolog/src/Monolog/Formatter/LineFormatter.php:43
PHP 22. MonologFormatterLineFormatter->normalize() /Users/whitebear/httproot/myapp/vendor/monolog/monolog/src/Monolog/Formatter/NormalizerFormatter.php:40
PHP 23. MonologFormatterNormalizerFormatter->normalize() /Users/whitebear/httproot/myapp/vendor/monolog/monolog/src/Monolog/Formatter/LineFormatter.php:88
PHP 24. MonologFormatterLineFormatter->normalize() /Users/whitebear/httproot/myapp/vendor/monolog/monolog/src/Monolog/Formatter/NormalizerFormatter.php:70
PHP 25. MonologFormatterNormalizerFormatter->normalize() /Users/whitebear/httproot/myapp/vendor/monolog/monolog/src/Monolog/Formatter/LineFormatter.php:88
Fatal error: Allowed memory size of 268435456 bytes exhausted (tried to allocate 72 bytes) in /Users/whitebear/httproot/myapp/vendor/monolog/monolog/src/Monolog/Formatter/NormalizerFormatter.php on line 62
Call Stack:
0.0002 229968 1. {main}() /Users/whitebear/httproot/myapp/app/console:0
0.0126 2063376 2. SymfonyComponentConsoleApplication->run() /Users/whitebear/httproot/myapp/app/console:22
0.0144 2256968 3. SymfonyBundleFrameworkBundleConsoleApplication->doRun() /Users/whitebear/httproot/myapp/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:106
0.1503 11751600 4. SymfonyComponentConsoleApplication->doRun() /Users/whitebear/httproot/myapp/vendor/symfony/symfony/src/Symfony/Bundle/FrameworkBundle/Console/Application.php:83
0.1518 11753384 5. SymfonyComponentConsoleCommandCommand->run() /Users/whitebear/httproot/myapp/vendor/symfony/symfony/src/Symfony/Component/Console/Application.php:200
0.1521 11758040 6. AcmeTopBundleCommandtwCrawlerCommand->execute() /Users/whitebear/httproot/myapp/vendor/symfony/symfony/src/Symfony/Component/Console/Command/Command.php:242
0.2409 20673112 7. AcmeTopBundleCommandtwCrawlerCommand->initialRun() /Users/whitebear/httproot/myapp/src/Acme/TopBundle/Command/twCrawlerCommand.php:430
101.5883 235069104 8. EntityManager52ee6dcbdb2bb_546a8d27f194334ee012bfe64f629947b07e4919__CG__DoctrineORMEntityManager->flush() /Users/whitebear/httproot/myapp/src/Acme/TopBundle/Command/twCrawlerCommand.php:284
101.5883 235069200 9. DoctrineORMEntityManager->flush() /Users/whitebear/httproot/myapp/app/cache/dev/jms_diextra/doctrine/EntityManager_52ee6dcbdb2bb.php:402
101.5883 235069280 10. DoctrineORMUnitOfWork->commit() /Users/whitebear/httproot/myapp/vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php:389
107.0744 259211408 11. DoctrineORMUnitOfWork->executeInserts() /Users/whitebear/httproot/myapp/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:335
107.1128 259277872 12. DoctrineORMPersistersBasicEntityPersister->executeInserts() /Users/whitebear/httproot/myapp/vendor/doctrine/orm/lib/Doctrine/ORM/UnitOfWork.php:952
109.1604 267877360 13. DoctrineDBALStatement->execute() /Users/whitebear/httproot/myapp/vendor/doctrine/orm/lib/Doctrine/ORM/Persisters/BasicEntityPersister.php:279
109.1604 267877408 14. DoctrineDBALLoggingLoggerChain->startQuery() /Users/whitebear/httproot/myapp/vendor/doctrine/dbal/lib/Doctrine/DBAL/Statement.php:161
109.1604 267877408 15. SymfonyBridgeDoctrineLoggerDbalLogger->startQuery() /Users/whitebear/httproot/myapp/vendor/doctrine/dbal/lib/Doctrine/DBAL/Logging/LoggerChain.php:54
109.1604 267877592 16. SymfonyBridgeDoctrineLoggerDbalLogger->log() /Users/whitebear/httproot/myapp/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php:80
109.1604 267877592 17. MonologLogger->debug() /Users/whitebear/httproot/myapp/vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/Logger/DbalLogger.php:102
109.1604 267877640 18. MonologLogger->addRecord() /Users/whitebear/httproot/myapp/vendor/monolog/monolog/src/Monolog/Logger.php:423
109.1604 267878968 19. MonologHandlerAbstractProcessingHandler->handle() /Users/whitebear/httproot/myapp/vendor/monolog/monolog/src/Monolog/Logger.php:244
109.1604 267878968 20. MonologFormatterLineFormatter->format() /Users/whitebear/httproot/myapp/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php:35
109.1604 267878968 21. MonologFormatterNormalizerFormatter->format() /Users/whitebear/httproot/myapp/vendor/monolog/monolog/src/Monolog/Formatter/LineFormatter.php:43
109.1604 267878968 22. MonologFormatterLineFormatter->normalize() /Users/whitebear/httproot/myapp/vendor/monolog/monolog/src/Monolog/Formatter/NormalizerFormatter.php:40
109.1604 267878968 23. MonologFormatterNormalizerFormatter->normalize() /Users/whitebear/httproot/myapp/vendor/monolog/monolog/src/Monolog/Formatter/LineFormatter.php:88
109.1605 267881872 24. MonologFormatterLineFormatter->normalize() /Users/whitebear/httproot/myapp/vendor/monolog/monolog/src/Monolog/Formatter/NormalizerFormatter.php:70
109.1605 267881872 25. MonologFormatterNormalizerFormatter->normalize() /Users/whitebear/httproot/myapp/vendor/monolog/monolog/src/Monolog/Formatter/LineFormatter.php:88
多亏了您的评论,我知道它的内存是$ this-> em
的恢复的。所以,我每5000次都将代码放置为$ this-> em;
if ($k > 1000){
$this->em->flush();
unset($this->em);
$this->em = $this->getContainer()->get('doctrine')->getManager();
}
但仍然徒劳地,我认为我应该阐明$ this-> getContainer() -> get('doctrine') -> getManager();
有可能吗?还是我是对的?
您可能需要阅读学说ORM批处理处理。他们正在使用这种模式:
// $em instanceof EntityManager
$batchSize = 20;
for ($i=1; $i<=10000; ++$i) {
$obj = new MyEntity;
$obj->setFoo('...');
// ... set more data
$em->persist($obj);
if (($i % $batchSize) == 0) {
$em->flush();
$em->clear();
}
}
$em->flush();
我理解 - 持久后,学说将所有映射的对象保存在身份图中的内存中。因此,每个新对象都会一直在记忆需求不断增长。如果您确实清除(),它将分离所有托管实体,如果您没有对它们的参考,它们将是垃圾收集的。
当心:它将分离您使用的所有其他实体。或者您只能分离那些插入的对象$em->detach($twNode); // detach from Doctrine, so that it can be GC'd immediately
编辑:循环之后添加了flush(),以确保即使实体计数不能按批量大小排除,也要保存所有实体,如注释中所建议的
您可以通过在脚本的顶部添加以下内容来避免这种情况
set_time_limit(6000);
ini_set("memory_limit", -1);
但是,实际问题应在编码中解决。
三种方法...
增加允许的内存
ini_set("memory_limit", "512M");
或php.ini文件本身将您的过程分解为一个块,例如每件5000。
弄清楚内存的去向,并尝试使用
unset
或将对象设置为null
。
一个很好的第一个查看您的内存要去的地方可能在这里
101.5883 235069104 8. EntityManager52ee6dcbdb2bb_546a8d27f194334ee012bfe64f629947b07e4919__CG__DoctrineORMEntityManager->flush() /Users/whitebear/httproot/myapp/src/Acme/TopBundle/Command/twCrawlerCommand.php:284
随着内存的量增加了约200mb。
if (($i % $batchSize) == 0) {
$em->flush();
$em->clear();
}
还不够。
我还需要:
$em->getConnection()->getConfiguration()->setSQLLogger(null);
然后还可以。