<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <p>This probably is a common mistake, however the Weak wrapper is
      also easy to use wrongly.  You can't just wrap it like you are
      doing in your example, because this is how the references look:</p>
    <p>     menuItem ---> WeakEventHandler ---weakly---> Lambda</p>
    <p>In effect, the Lambda is weakly referenced, and is the only
      reference, so it can be cleaned up immediately (or whenever the GC
      decides to run) and your menu item will stop working at a random
      time in the future.  The WeakEventHandler will remain, but only as
      a stub (and gets cleaned up when the listener list gets
      manipulated again at a later stage).</p>
    <p>The normal way to use a Weak wrapper is to put a reference to the
      wrapped part in a private field, which in your case would not
      solve the problem.<br>
    </p>
    <p>I'm assuming however that you are also removing the menu item
      from the Open Windows list. This menu item should be cleaned up
      fully, and so the reference to the Stage should also disappear. 
      I'm wondering why that isn't happening?  If the removed menu item
      remains referenced somehow, then it's Action will reference the
      Stage, which in turns keeps the Stage in memory.</p>
    <p>I'd look into the above first before trying other solutions.</p>
    <p>--John<br>
    </p>
    <p><br>
    </p>
    <div class="moz-cite-prefix">On 18/04/2024 17:50, Thiago Milczarek
      Sayão wrote:<br>
    </div>
    <blockquote type="cite"
cite="mid:CAAP_wu=WonAYkBVPSgMdUPEXUdFy+aW09xoJTKcNZ2iXkRZ9fQ@mail.gmail.com">
      <meta http-equiv="content-type" content="text/html; charset=UTF-8">
      <div dir="ltr">I was investigating,
        <div><br>
        </div>
        <div>It probably should be menuItem.setOnAction(new
          WeakEventHandler<>(e -> stage.toFront()));</div>
        <div><br>
        </div>
        <div>But I bet it's a common mistake. Maybe the setOnAction
          should mention it?</div>
        <div><br>
        </div>
        <div><br>
        </div>
      </div>
      <br>
      <div class="gmail_quote">
        <div dir="ltr" class="gmail_attr">Em qui., 18 de abr. de 2024 às
          11:54, Andy Goryachev <<a
            href="mailto:andy.goryachev@oracle.com"
            moz-do-not-send="true" class="moz-txt-link-freetext">andy.goryachev@oracle.com</a>>
          escreveu:<br>
        </div>
        <blockquote class="gmail_quote" style="margin:0px 0px 0px
          0.8ex;border-left:1px solid rgb(204,204,204);padding-left:1ex">
          <div class="msg9028551156355543720">
            <div style="overflow-wrap: break-word;" lang="EN-US">
              <div class="m_-1465540377573045932WordSection1">
                <p class="MsoNormal"><span
                    style="font-size:11pt;font-family:"Iosevka
                    Fixed SS16"">You are correct - the lambda
                    strongly references `stage` and since it is in turn
                    is strongly referenced from the menu item it creates
                    a leak.</span></p>
                <p class="MsoNormal"><span
                    style="font-size:11pt;font-family:"Iosevka
                    Fixed SS16""> </span></p>
                <p class="MsoNormal"><span
                    style="font-size:11pt;font-family:"Iosevka
                    Fixed SS16"">The lambda is essentially this:</span></p>
                <p class="MsoNormal"><span
                    style="font-size:11pt;font-family:"Iosevka
                    Fixed SS16""> </span></p>
                <p class="MsoNormal"><span
                    style="font-size:11pt;font-family:"Iosevka
                    Fixed SS16"">menuItem.setOnAction(new
                    H(stage));</span></p>
                <p class="MsoNormal"><span
                    style="font-size:11pt;font-family:"Iosevka
                    Fixed SS16"">class $1 implements
                    EventHandler<ActionEvent> {</span></p>
                <p class="MsoNormal"><span
                    style="font-size:11pt;font-family:"Iosevka
                    Fixed SS16"">  private final Stage stage;</span></p>
                <p class="MsoNormal"><span
                    style="font-size:11pt;font-family:"Iosevka
                    Fixed SS16"">  public $1(Stage s) {</span></p>
                <p class="MsoNormal"><span
                    style="font-size:11pt;font-family:"Iosevka
                    Fixed SS16"">    this.stage = s; // holds the
                    reference and causes the leak</span></p>
                <p class="MsoNormal"><span
                    style="font-size:11pt;font-family:"Iosevka
                    Fixed SS16"">  }</span></p>
                <p class="MsoNormal"><span
                    style="font-size:11pt;font-family:"Iosevka
                    Fixed SS16"">  public void handle(ActionEvent
                    ev) {</span></p>
                <p class="MsoNormal"><span
                    style="font-size:11pt;font-family:"Iosevka
                    Fixed SS16"">    stage.toFront();</span></p>
                <p class="MsoNormal"><span
                    style="font-size:11pt;font-family:"Iosevka
                    Fixed SS16"">  }</span></p>
                <p class="MsoNormal"><span
                    style="font-size:11pt;font-family:"Iosevka
                    Fixed SS16"">}</span></p>
                <p class="MsoNormal"><span
                    style="font-size:11pt;font-family:"Iosevka
                    Fixed SS16""> </span></p>
                <p class="MsoNormal"><span
                    style="font-size:11pt;font-family:"Iosevka
                    Fixed SS16"">-andy</span></p>
                <p class="MsoNormal"><span
                    style="font-size:11pt;font-family:"Iosevka
                    Fixed SS16""> </span></p>
                <div
                  id="m_-1465540377573045932mail-editor-reference-message-container">
                  <div>
                    <div
style="border-right:none;border-bottom:none;border-left:none;border-top:1pt
                      solid rgb(181,196,223);padding:3pt 0in 0in">
                      <p class="MsoNormal" style="margin-bottom:12pt"><b><span
                            style="font-size:12pt;color:black">From:
                          </span></b><span
                          style="font-size:12pt;color:black">openjfx-dev
                          <<a
                            href="mailto:openjfx-dev-retn@openjdk.org"
                            target="_blank" moz-do-not-send="true"
                            class="moz-txt-link-freetext">openjfx-dev-retn@openjdk.org</a>>
                          on behalf of Thiago Milczarek Sayão <<a
                            href="mailto:thiago.sayao@gmail.com"
                            target="_blank" moz-do-not-send="true"
                            class="moz-txt-link-freetext">thiago.sayao@gmail.com</a>><br>
                          <b>Date: </b>Thursday, April 18, 2024 at
                          03:42<br>
                          <b>To: </b>openjfx-dev <<a
                            href="mailto:openjfx-dev@openjdk.org"
                            target="_blank" moz-do-not-send="true"
                            class="moz-txt-link-freetext">openjfx-dev@openjdk.org</a>><br>
                          <b>Subject: </b>Possible leak on setOnAction</span></p>
                    </div>
                    <div>
                      <p class="MsoNormal"><span style="font-size:12pt">Hi,</span></p>
                      <div>
                        <p class="MsoNormal"><span
                            style="font-size:12pt"> </span></p>
                      </div>
                      <div>
                        <p class="MsoNormal"><span
                            style="font-size:12pt">I'm pretty sure
                            setOnAction is holding references. </span></p>
                      </div>
                      <div>
                        <p class="MsoNormal"><span
                            style="font-size:12pt"> </span></p>
                      </div>
                      <div>
                        <p class="MsoNormal"><span
                            style="font-size:12pt">I have a "Open
                            Windows" menu on my application where it
                            lists the Stages opened and if you click, it
                            calls stage.toFront():</span></p>
                      </div>
                      <div>
                        <p class="MsoNormal"><span
                            style="font-size:12pt"> </span></p>
                      </div>
                      <div>
                        <p class="MsoNormal"><span
                            style="font-size:12pt">menuItem.seOnAction(e
                            -> stage.toFront())</span></p>
                      </div>
                      <div>
                        <p class="MsoNormal"><span
                            style="font-size:12pt"> </span></p>
                      </div>
                      <div>
                        <p class="MsoNormal"><span
                            style="font-size:12pt">I had many crash
                            reports, all OOM. I got the hprof files and
                            analyzed them - turns out this was holding
                            references to all closed stages.</span></p>
                      </div>
                      <div>
                        <p class="MsoNormal"><span
                            style="font-size:12pt"> </span></p>
                      </div>
                      <div>
                        <p class="MsoNormal"><span
                            style="font-size:12pt">To fix it, I call
                            setOnAction(null) when the stage is closed.</span></p>
                      </div>
                      <div>
                        <p class="MsoNormal"><span
                            style="font-size:12pt"> </span></p>
                      </div>
                      <div>
                        <p class="MsoNormal"><span
                            style="font-size:12pt">I will investigate
                            further and provide an example.</span></p>
                      </div>
                      <div>
                        <p class="MsoNormal"><span
                            style="font-size:12pt"> </span></p>
                      </div>
                      <div>
                        <p class="MsoNormal"><span
                            style="font-size:12pt">-- Thiago.</span></p>
                      </div>
                    </div>
                  </div>
                </div>
              </div>
            </div>
          </div>
        </blockquote>
      </div>
    </blockquote>
  </body>
</html>