Fixing “Allowed memory size exhausted” in Symfony: When Doctrine Metadata Crashes Your Serializer

PHP Symphony Serializer
PHP Symphony Serializer

If you have ever encountered a vague but catastrophic error like this:

… while working in a Symfony project using Doctrine and the Symfony serializer, you are not alone 🙂

This post walks you through a real-world debugging journey that ended up pinpointing a subtle but devastating issue: Symfony’s default serializer uses Doctrine metadata – and when used carelessly, it can eat up all your memory.

The Setup

I was working on a Symfony-based backend with the following stack:

  • Symfony Messenger
  • Doctrine ORM
  • Symfony Serializer
  • API Platform (used, but not involved in this case)
  • Docker + DDEV environment

I had a class, let’s name it RequestLog used to store large request/response payloads (e.g., for debugging or backup purposes). It looked like this:

The problem showed up when serializing large data into this entity and then trying to serialize the whole entity again.

The Symptom

During a background task, the memory consumption suddenly spiked and caused this error:

With the stack trace pointing deep into:

The Root Cause

After some head scratching, test scripts, and dead ends, it became clear:

Symfony’s default ObjectNormalizer uses Doctrine’s metadata extractor if present.

That means every entity being serialized pulls in its entire Doctrine metadata, including relationships, even if you don’t want or need them.

In our case, RequestLog had huge JSON fields that didn’t need metadata or special treatment.

The default serializer was overkill.

The Solution: A Lightweight Serializer Adapter

We created a dedicated, minimal serializer that avoids Doctrine metadata completely:

Then we injected this into our SerializerService and called it explicitly:

Takeaways

  • Symfony’s Serializer is powerful — but dangerous with Doctrine entities and large payloads.
  • Avoid using the default ObjectNormalizer when serializing internal debug or backup data.
  • Use lightweight, no-metadata serializers where appropriate.
  • Don’t rely solely on API Platform annotations to control exposure – the serializer is used everywhere.

Final Words

This issue is sneaky. It won’t crash in dev, and it might only surface under production-level loads. If you’re dealing with serialization of entities, especially those with large or nested data, consider this a warning.
Build your own serializer or explicitly strip metadata – and stay in control of memory usage.