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

Harshitha Onkar honkar at openjdk.org
Wed Jan 24 20:21:29 UTC 2024


On Wed, 24 Jan 2024 05:35:42 GMT, Prasanta Sadhukhan <psadhukhan at openjdk.org> wrote:

>> When using a TreeCellRenterer which creates new components in getTreeCellRendererComponent() in a JTree that is not visible, changes to the nodes cause a memory leak.
>> When a node is changed, the Method getNodeDimensions() is called to calculate the new dimensions for the node. In this method, getTreeCellRendererComponent() is called to obtain the renderer component (what else...) and [this component is added to rendererPane](https://github.com/openjdk/jdk/blob/36f4b34f1953af736706ec67192204727808bc6c/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTreeUI.java#L3283-L3284). It is not removed from the rendererPane afterwards. 
>> Only when the tree is painted, the paint() method does a removeAll on the rendererPane [in this code](https://github.com/openjdk/jdk/blob/36f4b34f1953af736706ec67192204727808bc6c/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTreeUI.java#L1500)
>> 
>> FIx is added to remove the components from rendererPane when the JTree UI is changed/uninstalled only when tree is not visible since they are already removed when tree is painted in paint() method..
>
> Prasanta Sadhukhan has updated the pull request incrementally with one additional commit since the last revision:
> 
>   Retain last component in rendererPane. Testcase added

test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java line 99:

> 97:         Thread updateThread = new Thread(new Runnable( ) {
> 98:             public void run( ) {
> 99:                 runChanges( );

Suggestion:

        jTree1.setCellRenderer(new TreeCellRenderer());
        Thread updateThread = new Thread(new Runnable() {
            public void run() {
                runChanges();


There are few other places in the test where the extra space needs to be removed.

test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java line 106:

> 104:         Thread infoThread = new Thread(new Runnable( ) {
> 105:             public void run( ) {
> 106:                 runInfo( );

Extra space

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

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


More information about the client-libs-dev mailing list