Stack Matrix Hub (9)
Preventing Doctrine ORM Hydrator & IdentityMap Memory Leaks
Stop batch product imports, Messenger workers, and FrankenPHP threads from ballooning past 900 MB RSS via IdentityMap clearing and DTO hydration.
EntityManager::clear() / 50 rows · Array/Scalar hydration · 75% less RSS
How Doctrine IdentityMap Retains Every Hydrated Product & Variant
Doctrine ORM caches every managed Product, ProductVariant, and ChannelPricing entity inside its UnitOfWork IdentityMap until clear() is invoked. Inside long-running FrankenPHP workers or CSV import commands, hydrating 5,000 SKUs with translations inflates a single PHP process past 850 MB.
Read-Only DTOs, Iterable Queries & Periodic UnitOfWork Clears
Use read-only scalar or DTO hydration for catalog feeds and export jobs, stream large queries with toIterable(), and call EntityManagerInterface::clear() every 25 to 50 entities inside Messenger handlers and CLI batch workers.
Measured Thresholds & Configuration
| Hydration Pattern (5,000 SKUs) | Peak PHP RSS | Execution Speed | Recommended Context |
|---|---|---|---|
| Full ORM Object Graph (no clear) | 880 - 1,150 MB | Slow GC pauses | Avoid in batch/workers |
| Batch flush() + clear() every 50 | 165 - 210 MB | Steady constant heap | Catalog CSV & Messenger imports |
| Read-Only Scalar / DTO Query | 64 - 95 MB | 3.2x faster | API feeds, sitemaps & exports |