RFR: 6507038: Memory Leak in JTree / BasicTreeUI [v4]

Alexey Ivanov aivanov at openjdk.org
Tue Jan 30 16:17:09 UTC 2024


On Tue, 30 Jan 2024 15:35:54 GMT, Alexey Ivanov <aivanov at openjdk.org> wrote:

>> Prasanta Sadhukhan has updated the pull request incrementally with one additional commit since the last revision:
>> 
>>   Use removeAll and testcase modified
>
> test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java line 204:
> 
>> 202:                 count++;
>> 203:             }
>> 204:         }
> 
> You have to iterate over `weakRefArrLabel` in a synchronized block. (No, declaring `weakRefArrLabel` as `volatile` is not enough.)
> Suggestion:
> 
>         synchronized (weakRefArrLabel) {
>             for (WeakReference<JLabel> ref : weakRefArrLabel) {
>                 if (ref.get() == null) {
>                     count++;
>                 }
>             }
>         }

Alternatively, you can use Stream API:


    private long getCleanedUpLabelCount() {
        synchronized (weakRefArrLabel) {
            return weakRefArrLabel.stream()
                                  .filter(r -> r.get() != null)
                                  .count();
        }
    }

-------------

PR Review Comment: https://git.openjdk.org/jdk/pull/17458#discussion_r1471461777


More information about the client-libs-dev mailing list