How to Fix Memory Leak in Doctrine Migrations
I had to write a Doctrine migration to fix some bad data. Unfortunately there was so much data (~600K records) that the migration leaked. The memory consumption reaches about 12Gb! That’s not really acceptable!
Usually, when there is a leak in Doctrine DBAL (not the ORM), it comes from the SQL logger. When you execute 600K SQL queries, there are 600K log records in the memory!
With Doctrine DBAL 2, we can write the following line of code to avoid the leak:
$this->connection->getConfiguration()->setSQLLogger(null);
However, this doesn’t work anymore with Doctrine DBAL 3! Now they have a middleware system to build the connection. All middlewares wrap the driver to add more capabilities.
So, to fix the migration, I decided to remove all middlewares and create a new connection:
final class Version20230206154337 extends AbstractMigration
{
public function up(Schema $schema): void
{
$this->connection->getConfiguration()->setMiddlewares([]);
$this->connection = DriverManager::getConnection(
$this->connection->getParams(),
$this->connection->getConfiguration(),
);
// regular doctrine migration code
}
Obviously, removing all middlewares can be a bad idea, but in my case only the followings were registered:
Doctrine\DBAL\Logging\MiddlewareDoctrine\Bundle\DoctrineBundle\Middleware\DebugMiddlewareSentry\SentryBundle\Tracing\Doctrine\DBAL\TracingDriverMiddleware
Each middleware brings debug, so it’s safe to remove them all. But some colleagues pushed me to find the culprit, and you may be surprised, but it’s the DebugMiddleware!
If you prefer to remove only this middleware, you can use the following code:
$middlewares = array_filter(
$this->connection->getConfiguration()->getMiddlewares(),
fn ($middleware) => !$middleware instanceof \Doctrine\Bundle\DoctrineBundle\Middleware\DebugMiddleware
);
$this->connection->getConfiguration()->setMiddlewares($middlewares);
I hope this article can help you to reduce memory leak when dealing with a big amount of data.
Commentaires et discussions
Ces clients ont profité de notre expertise
Ouibus a pour ambition de devenir la référence du transport en bus longue distance. Dans cette optique, les enjeux à venir de la compagnie sont nombreux (vente multi-produit, agrandissement du réseau, diminution du time-to-market, amélioration de l’expérience et de la satisfaction client) et ont des conséquences sur la structuration de la nouvelle…
Après avoir monté une nouvelle équipe de développement, nous avons procédé à la migration de toute l’infrastructure technique sur une nouvelle architecture fortement dynamique à base de Symfony2, RabbitMQ, Elasticsearch et Chef. Les gains en performance, en stabilité et en capacité de développement permettent à l’entreprise d’engager de nouveaux marchés…
Pour renforcer Ouranos, le référentiel de données dédié à la signalisation ferroviaire, SNCF Réseau a sollicité JoliCode afin d’auditer et améliorer la qualité technique de l’application. Développée en Symfony et API Platform, Ouranos est au cœur des processus de production, utilisé aussi bien en interne que par l’industrie. Notre intervention s’est…