-
Notifications
You must be signed in to change notification settings - Fork 5.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
6507038: Memory Leak in JTree / BasicTreeUI #17458
Conversation
👋 Welcome back psadhukhan! A progress list of the required criteria for merging this PR into |
Webrevs
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ran the reproducer attached to JBS without and with the fix. The "Live Label count" seem to increase in both cases.
After running the test, I switched to tab 2, which makes tab 1 (with JTree) inactive but during this time the Live label count increases.
Will a test case be added as part of this fix?
Live TestLabels:0
Live TestLabels:74
Live TestLabels:57
Live TestLabels:21
Live TestLabels:60
Live TestLabels:46
Live TestLabels:62
Live TestLabels:102
Live TestLabels:140
Live TestLabels:180
Live TestLabels:218
Live TestLabels:256
Live TestLabels:296
Live TestLabels:334
Live TestLabels:374
Live TestLabels:412
Live TestLabels:452
Live TestLabels:490
As mentioned in the comment jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTreeUI.java Lines 3283 to 3284 in 36f4b34
the components are to be removed when UI changes, so I guess it is expected to increase if UI is not changing (ie. in case tree is not visible) so I have removed at end when component is uninstalled at which point it is a leak too, if not removed.. A testcase is hard to provide for this leak..it's all notional.. |
can we trigger this memory leak by some new or existed tests? |
I cant see any nor can I think of any..you have any idea? |
@@ -1334,6 +1334,9 @@ protected void uninstallKeyboardActions() { | |||
*/ | |||
protected void uninstallComponents() { | |||
if(rendererPane != null) { | |||
if (!tree.isVisible()) { | |||
rendererPane.removeAll(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if( -> if (
Is it actually necessary to check visibility ?
But more importantly, in the case of the test in the bug, how and when does this uninstallComponents() method get called ? It isn't clear to me how it changes the reported behaviour of that test.
I thought uninstallComponents() is just part of uninstallUI() like when tearing down and freeing the tree, or at least switching L&F .. whereas the submitter seems to be implying nodeChanged() is the problem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the submitter mentioned during nodeChanged() but I mentioned it before in this comment
that the comment present, implied it's not an issue, if the component is not removed if UI is not changed (as it is in the case when tree is not visible)...I am not sure why it was added and when but should we ignore that comment and try to remove the component even if tree is not visible (and UI is not changing)?
I went with the thought that the coder meant what he said and I freed the components at the end (as you told during tearing down) so that there is no leak..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My point is not about whether this is the only case we need to address, I am asking what harm could come from removing the isVisible() test here ?
if (!tree.isVisible()) { | ||
rendererPane.removeAll(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Wouldn't it be enough to remove rendererPane
from and assign null
to it?
It's done this way in BasicTableUI
and BasicListUI
:
jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTableUI.java
Lines 1656 to 1658 in 0d8543d
table.remove(rendererPane); | |
rendererPane = null; | |
table = null; |
and
jdk/src/java.desktop/share/classes/javax/swing/plaf/basic/BasicListUI.java
Lines 986 to 988 in 0d8543d
list.remove(rendererPane); | |
rendererPane = null; | |
list = null; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is already been done in completeUIUninstall
called from uninstallUI
rendererPane.removeAll
is done to remove all components that are added in getNodeDImensions
which gets removed in paint
but if tree is not visible, then paint
is not called so those components are not removed, which I am trying to do now in uninstallUI
by checking tree visibility condition
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay, I see it. Then the added lines in uninstallComponents
aren't needed because rendererPane
is assigned null
in completeUIUninstall
which is called right after uninstallComponents
, therefore rendererPane
itself and all the components added into it are eligible for garbage collection.
Where is the memory leak then?
As far as I understand, the renderer component is leaked. Usually, the renderer isn't re-created each time After |
This is an interesting part… If the renderer component is added over and over again into To me, |
I am not sure what "notional" means - do you mean there is no actual leak ? But if somehow - as discussed above repeated calls to renderPane.add(Component) accumulate components If having 5 makes no sense, can we (should we ?) stop more than one from being added ? |
Yes, seems like rendererPane should retain the last rendering component..No regression is observed in SwingSet2 or in other tree tests.. |
test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java
Outdated
Show resolved
Hide resolved
test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java
Outdated
Show resolved
Hide resolved
test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java
Outdated
Show resolved
Hide resolved
src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTreeUI.java
Outdated
Show resolved
Hide resolved
smCount++; | ||
} | ||
|
||
public void finalize( ) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The finalize
method is deprecated. Can't we use Reference
API for this purpose?
Store a list of PhantomReference<JLabel>
. The size of the list is the number of JLabel
objects created. Once you receive a reference via the ReferenceQueue
, remove it from the list.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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..
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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: aivanov-jdk@b0da8b3
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.
jdk/test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java
Lines 193 to 201 in b0da8b3
synchronized (referenceList) { | |
start = referenceList.size(); | |
Reference<?> ref; | |
while ((ref = referenceQueue.poll()) != null) { | |
referenceList.remove(ref); | |
removed++; | |
} | |
left = referenceList.size(); | |
} |
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'll approve it if you don't want to use PhantomReference
.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for your "reference" on PhantomReference..Although my testcode is doing same thing (which you acknowledged) no harm in trying new way (for me) for my future reference so have updated the test to use PhantomReference.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you add me as contributor, please?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It didn't work. It would've, if you'd added a blank between the two line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let me retry
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
/contributor add @aivanov-jdk
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks like it needs to be a top-level comment rather than a reply to code change discussion.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
hmm...it is a comment..should be parsed accordingly, I presumed...let me retry..
test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java
Outdated
Show resolved
Hide resolved
test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java
Outdated
Show resolved
Hide resolved
test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java
Show resolved
Hide resolved
test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java
Outdated
Show resolved
Hide resolved
test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java
Outdated
Show resolved
Hide resolved
test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java
Outdated
Show resolved
Hide resolved
src/java.desktop/share/classes/javax/swing/plaf/basic/BasicTreeUI.java
Outdated
Show resolved
Hide resolved
test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java
Outdated
Show resolved
Hide resolved
test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java
Outdated
Show resolved
Hide resolved
label.setBackground(getBackgroundNonSelectionColor()); | ||
} | ||
|
||
weakRefArrLabel.add(smCount++, new WeakReference<JLabel>(label)); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
weakRefArrLabel.add(smCount++, new WeakReference<JLabel>(label)); | |
synchronized (weakRefArrLabel) { | |
weakRefArrLabel.add(new WeakReference<JLabel>(label)); | |
} |
You have to add items in a synchronized block; you read from the list in another thread. (Reading has be done inside a synchronized block too.)
Just add to the end of the list.
test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java
Outdated
Show resolved
Hide resolved
SwingUtilities.invokeAndWait(new Runnable( ) { | ||
public void run( ) { | ||
DefaultTreeModel model = (DefaultTreeModel) jTree1.getModel(); | ||
TreeNode root = (TreeNode) model.getRoot(); | ||
DefaultMutableTreeNode n = (DefaultMutableTreeNode) model.getChild(root, 0); | ||
n.setUserObject("runcount " + currentCount); | ||
model.nodeChanged(n); | ||
} | ||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The anonymous class can be a lambda expression.
The code inside the Runnable
can be reduced to
n.setUserObject("runcount " + currentCount);
model.nodeChanged(n);
This implies, you make n
a field and initialise it in initComponents
; or after calling initComponents
.
for (WeakReference<JLabel> ref : weakRefArrLabel) { | ||
if (ref.get() == null) { | ||
count++; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You have to iterate over weakRefArrLabel
in a synchronized block. (No, declaring weakRefArrLabel
as volatile
is not enough.)
for (WeakReference<JLabel> ref : weakRefArrLabel) { | |
if (ref.get() == null) { | |
count++; | |
} | |
} | |
synchronized (weakRefArrLabel) { | |
for (WeakReference<JLabel> ref : weakRefArrLabel) { | |
if (ref.get() == null) { | |
count++; | |
} | |
} | |
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Alternatively, you can use Stream API:
private long getCleanedUpLabelCount() {
synchronized (weakRefArrLabel) {
return weakRefArrLabel.stream()
.filter(r -> r.get() != null)
.count();
}
}
test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java
Outdated
Show resolved
Hide resolved
test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java
Outdated
Show resolved
Hide resolved
test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java
Outdated
Show resolved
Hide resolved
rendererPane.removeAll(); | ||
// Only ever removed when UI changes, this is OK! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The comment no longer applies—you remove all the components from rendererPane
on the line above.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the only important thing that prevents me from approving this PR. The comment is misleading now.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
OK, modified
test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java
Outdated
Show resolved
Hide resolved
test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java
Outdated
Show resolved
Hide resolved
// Remove previously added components to prevent leak | ||
// and add the current component returned by cellrenderer for | ||
// painting and other measurements |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think the comment is redundant.
@prsadhuk This change now passes all automated pre-integration checks. ℹ️ This project also has non-automated pre-integration requirements. Please see the file CONTRIBUTING.md for details. After integration, the commit message for the final commit will be:
You can use pull request commands such as /summary, /contributor and /issue to adjust it as needed. At the time when this comment was updated there had been 451 new commits pushed to the
As there are no conflicts, your changes will automatically be rebased on top of these commits when integrating. If you prefer to avoid this automatic rebasing, please check the documentation for the /integrate command for further details. ➡️ To integrate this PR with the above commit message to the |
/contributor add @aivanov-jdk |
@prsadhuk |
private void initComponents() { | ||
jTabbedPane1 = new javax.swing.JTabbedPane(); | ||
jPanel1 = new javax.swing.JPanel(); | ||
jScrollPane1 = new javax.swing.JScrollPane(); | ||
jTree1 = new javax.swing.JTree(); | ||
jPanel2 = new javax.swing.JPanel(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fully qualified class name are redundant here since we already have the required imports.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ok
/* | ||
* @test | ||
* @bug 6507038 | ||
* @key headful | ||
* @summary Verifies memory leak in BasicTreeUI TreeCellRenderer | ||
* @run main TreeCellRendererLeakTest | ||
*/ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
jtreg header can be moved to before class declaration location.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No consensus on it yet so kept it same..
test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java
Outdated
Show resolved
Hide resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changes look good apart from the minor formatting issues mentioned above.
The updated test works well with the fix.
test/jdk/javax/swing/plaf/basic/BasicTreeUI/TreeCellRendererLeakTest.java
Outdated
Show resolved
Hide resolved
/integrate |
Going to push as commit e8ceb71.
Your commit was automatically rebased without conflicts. |
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. 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
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..
Progress
Issue
Reviewers
Contributors
<aivanov@openjdk.org>
Reviewing
Using
git
Checkout this PR locally:
$ git fetch https://git.openjdk.org/jdk.git pull/17458/head:pull/17458
$ git checkout pull/17458
Update a local copy of the PR:
$ git checkout pull/17458
$ git pull https://git.openjdk.org/jdk.git pull/17458/head
Using Skara CLI tools
Checkout this PR locally:
$ git pr checkout 17458
View PR using the GUI difftool:
$ git pr show -t 17458
Using diff file
Download this PR as a diff file:
https://git.openjdk.org/jdk/pull/17458.diff
Webrev
Link to Webrev Comment