Avoid retaining class files in annotation metadata - #37112
Merged
bclozel merged 1 commit intoAug 5, 2026
Conversation
bclozel
force-pushed
the
classfile-annotation-source-retention
branch
from
August 5, 2026 08:07
ef51942 to
2706399
Compare
ClassFileAnnotationDelegate passed the raw java.lang.classfile Annotation to MergedAnnotation.of() as the annotation source. That annotation holds a Utf8Entry, so retaining the metadata of an annotated class also retained its class file byte[], the parsed constant pool, and the class model. Store the declaring class name instead, as the ASM variant does. See spring-projectsgh-37111 Signed-off-by: Boris Perović <boris.perovic@sysdig.com>
bclozel
force-pushed
the
classfile-annotation-source-retention
branch
from
August 5, 2026 08:07
2706399 to
b556766
Compare
Member
|
Thanks @perovic ! |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This is the Class-File versus ASM metadata overhead case from #36737, reported with a reproducer in #37111. On Java 24+
MetadataReaderFactoryDelegateselects the Class-File reader, which retains considerably more heap than the ASM one for the same classes.ClassFileAnnotationDelegatekeeps the rawjava.lang.classfile.Annotationas theMergedAnnotationsource. That annotation holds aUtf8Entry, so retaining the metadata of an annotated class also retains its class filebyte[], the parsed constant pool, and the class model. This stores the declaring class name instead, which is whatSimpleAnnotationMetadataReadingVisitor.Sourceholds on the ASM path.Measured with the reproducer (Temurin 25.0.3, spring-core 7.0.8):
SimpleMetadataReaderFactory(ASM)ClassFileMetadataReaderFactorybeforeClassFileMetadataReaderFactoryafterIn a large application's test context this took the retained size of a single
DefaultListableBeanFactoryfrom 128.4 MB to 53.4 MB. Forcing the ASM reader in the same build, by copyingMetadataReaderFactoryDelegateinto the project as suggested here, gives 46.1 MB.The difference that is left is not addressed here.
ClassFileMethodMetadataholdsAccessFlagsandMethodTypeDescobjects andClassFileAnnotationMetadataoneAccessFlags, whereSimpleMethodMetadataandSimpleAnnotationMetadatakeep plainintandStringvalues instead. #36978 already makes that change forClassFileMethodMetadata. Once both are changed, the reproducer matches the ASM baseline of 12.3 MB and the test context mentioned above comes to 46.9 MB.