<Swing Dev> Is the following code supposed to leak?

Roman Kennke roman at kennke.org
Sat Aug 20 12:21:16 UTC 2011


Hi Clemens,

Am Samstag, den 20.08.2011, 11:54 +0200 schrieb Clemens Eisserer:
> Hi Roman,
> 
>         You keep creating and adding new listeners without ever
>         removing them.
>         Of course this is going to leak. 
> 
> What makes me wonder, I add the listeners to an Action from a newly
> created JTextArea.

Ah, in this case it should not leak. However, I don't know exactly what
effect that reference to the enclosing stackframe has, for this it would
be interesting to look at the de-assembled bytecode.

Roman

> Shouldn't the action belong to the JTextArea itself and be
> garbage-collected too when the JTextArea is no longer reachable? (like
> its the case for Listeners attached to the Component directly like
> Mouse/Key/...-Listeners)
> In the case of Notepad, that would mean Swing would require the
> application to remove the listeners manually?
> 
> I have to admit I don't understand the concept of Actions in detail...
> 
>  
>         It's not even related to the fact that
>         the listener references the array (although that could play a
>         role in
>         more complicated leaking scenarios)
>  
> Sure, the array is just there to trigger the OOM faster ;). 
> 
> 
> Thanks, Clemens
> 





More information about the swing-dev mailing list