Description
Philippe Julien opened SPR-17527 and commented
org.springframework.core.type.classreading.CachingMetadataReaderFactory.clearCache() was modified in Spring 5 to only clear the metaDataReaderCache of the LocalResourceCache instance type. When created with the constructor that takes a ResourceLoader as an argument, CachingMetadataReaderFactory will keep a strong reference on the Map that it got from the DefaultResourceLoader. This would be fine if DefaultResourceLoader.clearResourceCaches(), that is called after a context refresh, cleared the resourceCaches and the inner map that it contains, but this is not the case. So CachingMetadataReaderFactory.metaDataReaderCache remain in memory after context refresh.
In our application this increased the memory footprint by about 100mb after we updated to Spring 5.
A possible fix would be for CachingMetadataReaderFactory.clearCache() to set its metadataReaderCache to null if it's not an instance of LocalResourceCache.
Affects: 5.0.10, 5.1.2
Issue Links:
- CachingMetadataReaderFactory metadataReaderCache isn't cleaned up after context refresh when using the resource loader cache [SPR-17526] #22058 CachingMetadataReaderFactory metadataReaderCache isn't cleaned up after context refresh when using the resource loader cache ("is duplicated by")
Referenced from: commits 23d1049, 262c702
Backported to: 5.0.11