While recently working on a spring boot application, I noticed a certain prototyped bean is causing a severe memory leak.
The bean in question was created at runtime using ApplicationContext.getBean(...) However, when the bean falls out of scope, the FastClass subclasses/proxies created for the Enhancer and the bean itself still persists and never GCed. This is very strange since the bean and its Enhancer are already GCed. Does anyone every encounter this issue, or have any idea what the cause might be?
#Memory leak with prototype scoped beans
23 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @pseudo onyx! Please use
/closeor theClose Postbutton above when you're finished. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
⛹️♂️
what does that mean, friend
try using a memory profiler to identify any strong references to the bean!
or
Eclipse Memory Analyzer to analyze a heap dump!
I have done that, the root of the problem seems to be spring cglib creating a new enhancer for every prototype bean! This is a disaster because the enhancer has many static method proxies which never dies for as long as the Class is being referenced. I have no idea why spring creates so many enhancer classes though.
hmmm........got it!
if CGLIB is creating a new Enhancer for every prototype bean, this could potentially lead to a memory leak if the Enhancer and its associated proxies are not being properly garbage collected!
hit me Dms!
Hello friend, I'd much rather discuss potential fixes here. In case other people might find it helpful
alright!
first create the prototyype beans to see if there is any unnnecessary or excessive creation of beans!
are you there??
💤 Post marked as dormant
This post has been inactive for over 300 minutes, thus, it has been archived.
If your question was not answered yet, feel free to re-open this post or create a new one.
apologies, it was quite late i went to sleep.
it turns out, spring boot devtools lib makes spring use a RestartClassLoader to create its beans. this class loader is considered a "smart" and reloadable, thus the prototype bean proxies are cached but never reused
i will see if i can completely disable this behavior and see if it's fixed
issue appears to be fixed. ReloadClassLoader was the culprit
aaaah
cool