Description
Symfony version(s) affected
All
Description
In our application we have some large Symfony forms, with many nested collections, which result in hundreds of megabytes of memory usage. We make use of Symfony forms heavily, using events, extensions etc... so I expect there's a lot going on here, but we are looking for ways to reduce this memory usage significantly.
Debugging hasn't led to many obvious problems, just fairly linear increase in memory usage as the form builds.
How to reproduce
The following repository creates a form with a number of nested collections, and reports the memory usage. If the SubLocationType
field contains any options then memory usage jumps up.
- https://github.com/owsy/forms-memory-usage
- https://github.com/owsy/forms-memory-usage/blob/07e071e3ea8d164b752bf96572c4655c9d875989/src/Form/Type/SubLocationType.php#L15

Total memory usage of reproducer, with options added in SubLocationType.
A good deal of this memory usage seems to come from the upload_max_size_message
option in the core FormType
, the Closure holding onto memory. Overriding this to just return a string dramatically reduces memory usage from OptionsResolver.

Memory usage reduced for OptionsResolver, but still quite a lot
Possible Solution
I'm not familiar enough with this component to have any suggested solutions, but it does a bunch of cloning which looks like it may contribute to this (though may be entirely valid functionality).
Additional Context
Any suggestions are appreciated on areas to check, if others have tackled similar problems, etc...