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

Alexey Ivanov aivanov at openjdk.org
Tue Jan 30 10:40:42 UTC 2024


On Mon, 29 Jan 2024 06:16:04 GMT, Prasanta Sadhukhan <psadhukhan at openjdk.org> wrote:

>> test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java line 62:
>> 
>>> 60:         }
>>> 61: 
>>> 62:         public void finalize( ) {
>> 
>> The `finalize` method is deprecated. Can't we use `Reference` API for this purpose?
>> 
>> Store a list of [`PhantomReference<JLabel>`](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/ref/PhantomReference.html). The size of the list is the number of `JLabel` objects created. Once you receive a reference via the [`ReferenceQueue`](https://docs.oracle.com/en/java/javase/21/docs/api/java.base/java/lang/ref/ReferenceQueue.html), remove it from the list.
>
> I was not sure of the nuances of PhantomReference and ReferenceQueue, so have used WeakReference logic in the test which I think is more simpler..

It's simpler with `PhantomReference`. This type of reference does not allow getting its referent, and it's not needed in this case. When the referent of a reference is cleared by the GC, the reference is added into the associated reference queue. This is true for any type of `Reference`, but `PhantomReference` has to use a reference queue.

Here's how to use them: https://github.com/aivanov-jdk/jdk/commit/b0da8b30fdcf03abffa663eb5ba42df1fe4a9971

I keep the references in a list. The size of the list is the number of live `JLabel` objects. On each iteration, I poll the reference queue and remove the _dead_ references from the list.

https://github.com/openjdk/jdk/blob/b0da8b30fdcf03abffa663eb5ba42df1fe4a9971/test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java#L193-L201

If the number of removed references is zero, the test fails because objects are leaked. Otherwise, the test passes. This aligns with your latest changes.

Your code also does the job. I'd approve it if you don't want to use `PhantomReference`.

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

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


More information about the client-libs-dev mailing list