jfx icon indicating copy to clipboard operation
jfx copied to clipboard

8293444: Creating ScrollPane with same content component causes memory leak

Open andy-goryachev-oracle opened this issue 3 years ago • 4 comments

Using Weak*Listeners eliminates the memory leak.


Progress

  • [x] Change must not contain extraneous whitespace
  • [x] Commit message must refer to an issue
  • [ ] Change must be properly reviewed (2 reviews required, with at least 1 Reviewer, 1 Author)

Issue

  • JDK-8293444: Creating ScrollPane with same content component causes memory leak

Reviewing

Using git

Checkout this PR locally:
$ git fetch https://git.openjdk.org/jfx pull/900/head:pull/900
$ git checkout pull/900

Update a local copy of the PR:
$ git checkout pull/900
$ git pull https://git.openjdk.org/jfx pull/900/head

Using Skara CLI tools

Checkout this PR locally:
$ git pr checkout 900

View PR using the GUI difftool:
$ git pr show -t 900

Using diff file

Download this PR as a diff file:
https://git.openjdk.org/jfx/pull/900.diff

andy-goryachev-oracle avatar Sep 15 '22 17:09 andy-goryachev-oracle

:wave: Welcome back angorya! A progress list of the required criteria for merging this PR into master will be added to the body of your pull request. There are additional pull request commands available for use with this pull request.

bridgekeeper[bot] avatar Sep 15 '22 17:09 bridgekeeper[bot]

Webrevs

mlbridge[bot] avatar Sep 15 '22 21:09 mlbridge[bot]

/reviewers 2

kevinrushforth avatar Sep 16 '22 16:09 kevinrushforth

@kevinrushforth The total number of required reviews for this PR (including the jcheck configuration and the last /reviewers command) is now set to 2 (with at least 1 Reviewer, 1 Author).

openjdk[bot] avatar Sep 16 '22 16:09 openjdk[bot]

A small comment about the memory test with JMemoryBuddy. Instead of this:

<code>
List<WeakReference> refs = ...
WeakReference a = b;
refs.add(a);
<code>
for (WeakReference<ScrollPane> ref : refs) {
       JMemoryBuddy.checkCollectable(ref);
}

It is more readable this way:

JMemoryBuddy.memoryTest(checker -> {
  <code>
  checker.setAsReferenced(objects);
  checker.assertCollectable(b);
});

FlorianKirmaier avatar Sep 28 '22 10:09 FlorianKirmaier

A small comment about the memory test with JMemoryBuddy.

Thank you for suggestion. I will definitely use it in other tests! Here it won't work because in the part of the test one instance is expected to remain uncollected.

andy-goryachev-oracle avatar Sep 28 '22 17:09 andy-goryachev-oracle

if you use checker.setAsReferenced(object) then the specified object won't be collected for the current test. And there is also the statement assertNotCollectable() to check the reverse.

The method "checkCollectable/assertCollectable" is very slow in the negative case (but reliable in the positive case) The method "checkNotCollectable/<assertNotCollectable" should be used, if it's expected that the object is not collectable - otherwise the test will be quite slow.

FlorianKirmaier avatar Sep 28 '22 20:09 FlorianKirmaier

@FlorianKirmaier : I am not sure if using JMemoryBuddy.memoryTest results in a clearer or more concise code, given the nature of testScrollPaneObjLeakWhenUsedSameContent().

BTW, MemoryTestAPI names are a bit confusing: "assert*Collectable" suggests assertion is done at the point when these calls are made, though it looks like they just add a reference the an internal list. Also, assertCollectable might want to skip creation of a WeakReference if the object passed to it is already a WeakReference.

With your permission, I would like to keep this test as is.

andy-goryachev-oracle avatar Sep 28 '22 21:09 andy-goryachev-oracle

Of course, you can keep it this way.

The point of JMemoryBuddy.memoryTest is, that the user doesn't have to fiddle around with WeakReferences. For the record, your test might look like this (not tested):

    @Test
    public void testScrollPaneObjLeakWhenUsedSameContent() {
        JMemoryBuddy.memoryTest(() -> {
            BorderPane bp = new BorderPane();

            Stage stage = new Stage();
            stage.setScene(new Scene(bp));
            stage.show();

            Label content = new Label("content");

            ScrollPane sp1 = new ScrollPane(content);
            bp.setCenter(sp1);
            Toolkit.getToolkit().firePulse();

            ScrollPane sp2 = new ScrollPane(content);
            bp.setCenter(sp2);
            Toolkit.getToolkit().firePulse();

            bp.setCenter(null);
            Toolkit.getToolkit().firePulse();

            // When the label is still referenced, then only one ScrollPane should stay which is its parent
            checker.setAsReferenced(label)
            checker.assertCollectable(sp1)
            checker.assertNotCollectable(sp2)
        });
    }

    @Test
    public void testScrollPaneObjLeakWhenUsedSameContent() {
        JMemoryBuddy.memoryTest(() -> {
            BorderPane bp = new BorderPane();

            Stage stage = new Stage();
            stage.setScene(new Scene(bp));
            stage.show();

            Label content = new Label("content");

            ScrollPane sp1 = new ScrollPane(content);
            bp.setCenter(sp1);
            Toolkit.getToolkit().firePulse();

            ScrollPane sp2 = new ScrollPane(content);
            bp.setCenter(sp2);
            Toolkit.getToolkit().firePulse();

            bp.setCenter(null);
            Toolkit.getToolkit().firePulse();

            // IF our label is gone, both scrollpanes shold be collectable
            checker.assertCollectable(sp1)
            checker.assertCollectable(sp2)
        });
    }

I think only the first test is the important one.

FlorianKirmaier avatar Sep 29 '22 08:09 FlorianKirmaier

Thank you for clarifications, @FlorianKirmaier

andy-goryachev-oracle avatar Sep 29 '22 16:09 andy-goryachev-oracle

@andy-goryachev-oracle 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:

8293444: Creating ScrollPane with same content component causes memory leak

Reviewed-by: kcr, arapte

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 8 new commits pushed to the master branch:

  • cc00c8d5c7a73b9d9bc7c292ad51f8af9e63ff78: 8293795: [Accessibility] [Win] [Narrator] Exceptions when deleting text with continous key press in TextArea and TextField
  • 35675c8d27d54a26059b182614e18152794dbcec: 8293971: Loading new Media from resources can sometimes fail when loading from FXML
  • 03569b719b9d838d643dc4514971b06617275e86: 8293883: Add tests/.classpath (Eclipse)
  • 82db6cc342e7de4391f14c70877e77b0e52fbfff: 8289541: Update ICU4C to 71.1
  • 7c6a54d41945a21d2a4f0eceae56b5ba3df9f39f: 8293839: Documentation memory consistency effects of runLater
  • a27840e8fd7e79b1692488e4de7f6f5fdb930cf4: 8293368: GitHub Workflows security hardening
  • 5e4552db7389d93829e6ecfc61936ccad75c56c2: 8089280: horizontal scrollbar should never become visible in TableView with constrained resize policy
  • cef583eef41aff3db55542d5e90423eec5601f41: 8293214: Add support for Linux/LoongArch64

Please see this link for an up-to-date comparison between the source branch of this pull request and the master branch. 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.

As you do not have Committer status in this project an existing Committer must agree to sponsor your change. Possible candidates are the reviewers of this PR (@kevinrushforth, @arapte) but any other Committer may sponsor as well.

➡️ To flag this PR as ready for integration with the above commit message, type /integrate in a new comment. (Afterwards, your sponsor types /sponsor in a new comment to perform the integration).

openjdk[bot] avatar Oct 04 '22 16:10 openjdk[bot]

/integrate

andy-goryachev-oracle avatar Oct 04 '22 17:10 andy-goryachev-oracle

@andy-goryachev-oracle Your change (at version 0b22c2c958c7d996f33571c1c5ed269bf8ce2063) is now ready to be sponsored by a Committer.

openjdk[bot] avatar Oct 04 '22 17:10 openjdk[bot]

/sponsor

kevinrushforth avatar Oct 04 '22 18:10 kevinrushforth

Going to push as commit 337c78183be84a3420691f615096b54a68ac4300. Since your change was applied there have been 8 commits pushed to the master branch:

  • cc00c8d5c7a73b9d9bc7c292ad51f8af9e63ff78: 8293795: [Accessibility] [Win] [Narrator] Exceptions when deleting text with continous key press in TextArea and TextField
  • 35675c8d27d54a26059b182614e18152794dbcec: 8293971: Loading new Media from resources can sometimes fail when loading from FXML
  • 03569b719b9d838d643dc4514971b06617275e86: 8293883: Add tests/.classpath (Eclipse)
  • 82db6cc342e7de4391f14c70877e77b0e52fbfff: 8289541: Update ICU4C to 71.1
  • 7c6a54d41945a21d2a4f0eceae56b5ba3df9f39f: 8293839: Documentation memory consistency effects of runLater
  • a27840e8fd7e79b1692488e4de7f6f5fdb930cf4: 8293368: GitHub Workflows security hardening
  • 5e4552db7389d93829e6ecfc61936ccad75c56c2: 8089280: horizontal scrollbar should never become visible in TableView with constrained resize policy
  • cef583eef41aff3db55542d5e90423eec5601f41: 8293214: Add support for Linux/LoongArch64

Your commit was automatically rebased without conflicts.

openjdk[bot] avatar Oct 04 '22 18:10 openjdk[bot]

@kevinrushforth @andy-goryachev-oracle Pushed as commit 337c78183be84a3420691f615096b54a68ac4300.

:bulb: You may see a message that your pull request was closed with unmerged commits. This can be safely ignored.

openjdk[bot] avatar Oct 04 '22 18:10 openjdk[bot]