替代根據標準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