Ah I missed that point. Yeah, inner classes (anonymous/named), when 
instantiated, will hold a strong reference to the outer class, so as long as 
this class is in memory, the outer class cannot also be garbage collected. 
The fix looks fine for me.

Thanks,
Krishna

-----Original Message-----
From: Prasanta Sadhukhan 
Sent: Monday, August 6, 2018 9:31 PM
To: Krishna Addepalli <krishna.addepa...@oracle.com>; swing-dev@openjdk.java.net
Subject: Re: <Swing Dev> [12] RFR JDK-8204963:javax.swing.border.TitledBorder 
has a memory leak

Hi Krishna,

Anonymous class or non-static inner class holds an implicit reference to its 
outer class. In this case, the anonymous class is a listener so as long the 
listener is registered, it holds a reference to the instance of the outer 
class. Making the listener static will allow the outer instance to be garbage 
collected. Unfortunately, I guess Java does not support static anonymous class 
so it needs to be a static named class.

Regards
Prasanta
On 8/6/2018 8:27 PM, Krishna Addepalli wrote:
> Hi Prasanta,
>
> Could you explain the fix a bit more. AFAIK, the earlier code should also not 
> hold the reference to TitledBorder, longer than necessary.
> Whether an anonymous class, or a static named class, the UI manager would 
> maintain a strong reference to 
> PropertyChangeListener/TitledBorderPropertyChangeListener, which inturn 
> maintains a WeakReference to TitledBorder.
>
> Thanks,
> Krishna
>
> -----Original Message-----
> From: Prasanta Sadhukhan
> Sent: Monday, August 6, 2018 8:11 PM
> To: swing-dev@openjdk.java.net
> Subject: <Swing Dev> [12] RFR 
> JDK-8204963:javax.swing.border.TitledBorder has a memory leak
>
> Hi All,
>
> Please review a memory leak issue in javax.swing.border.TitledBorder where it 
> is seen that TitledBorder instances are never freed. This leak is an 
> aftereffect of JDK-8152159.
> This is because
> javax.swing.border.TitledBorder.installPropertyChangeListeners() creates an 
> instance of an anonymous inner class that implicitly holds a reference to the 
> containing TitledBorder class.
> Proposed fix is to make it a named static class so that it does not hold 
> reference and can be garbage collected.
>
> Bug:https://bugs.openjdk.java.net/browse/JDK-8204963
> webrev: http://cr.openjdk.java.net/~psadhukhan/8204963/webrev.0/
>
> 8152159 testcase also worked well.
>
> Regards
> Prasanta

Reply via email to