替代根据标准Doctrine2文档,您需要手动清除或增加实体。
除此之外,启用分析时(如在默认的开发环境中)。 Symfony2中的DoctrineBundle配置了几个记录器使用相当多的内存。您可以完全禁用日志记录,但它不是必需的。
一个有趣的副作用是记录仪同时影响Doctrine ORM和DBAL。记录器之一将导致使用默认记录器服务的任何服务的额外内存使用量。禁用所有这些将是理想的命令 - 因为分析器尚未使用。
这里是你可以做什么来禁用内存密集型记录器,同时保持谱在Symfony2中的其他部分启用:
$c = $this->getContainer();
/*
* The default dbalLogger is configured to keep "stopwatch" events for every query executed
* the only way to disable this, as of Symfony 2.3, Doctrine Bundle 1.2, is to reinistiate the class
*/
$dbalLoggerClass = $c->getParameter('doctrine.dbal.logger.class');
$dbalLogger = new $dbalLoggerClass($c->get('logger'));
$c->set('doctrine.dbal.logger', $dbalLogger);
// sometimes you need to configure doctrine to use the newly logger manually, like this
$doctrineConfiguration = $c->get('doctrine')->getManager()->getConnection()->getConfiguration();
$doctrineConfiguration->setSQLLogger($dbalLogger);
/*
* If profiling is enabled, this service will store every query in an array
* fortunately, this is configurable with a property "enabled"
*/
if($c->has('doctrine.dbal.logger.profiling.default'))
{
$c->get('doctrine.dbal.logger.profiling.default')->enabled = false;
}
/*
* When profiling is enabled, the Monolog bundle configures a DebugHandler that
* will store every log messgae in memory.
*
* As of Monolog 1.6, to remove/disable this logger: we have to pop all the handlers
* and then push them back on (in the correct order)
*/
$handlers = array();
try
{
while($handler = $logger->popHandler())
{
if($handler instanceOf \Symfony\Bridge\Monolog\Handler\DebugHandler)
{
continue;
}
array_unshift($handlers, $handler);
}
}
catch(\LogicException $e)
{
/*
* As of Monolog 1.6, there is no way to know if there's a handler
* available to pop off except for the \LogicException that's thrown.
*/
if($e->getMessage() != 'You tried to pop from an empty handler stack.')
{
/*
* this probably doesn't matter, and will probably break in the future
* this is here for the sake of people not knowing what they're doing
* so than an unknown exception is not silently discarded.
*/
// remove at your own risk
throw $e;
}
}
// push the handlers back on
foreach($handlers as $handler)
{
$logger->pushHandler($handler);
}
的D2 ORM层是不是真的设计用于大规模批量处理。使用DBAL层和使用数组可能会更好。 – Cerad 2012-03-14 20:22:05
与运行** - 无调试**有很大帮助(在调试模式下探查保留有关的每一个查询的信息在内存中) – arnaud576875 2012-09-14 11:29:56