Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8093738

[SwingNode] Calling setContent method repeatedly produces memory leak

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Not an Issue
    • Icon: P3 P3
    • 8u40
    • 8u5
    • javafx
    • os:win7/xp

      public class SwingFx extends Application {

          @Override
          public void start(Stage stage) {
              final SwingNode swingNode = new SwingNode();
              createAndSetSwingContent(swingNode);

              Button bt = new Button("Add New");
              bt.setOnMouseClicked(new EventHandler<MouseEvent>() {
                  @Override
                  public void handle(MouseEvent mouseEvent) {
                      swingNode.setContent(null);
                      swingNode.setContent(new TestComp());
                  }
              });
              BorderPane borderPane = new BorderPane();
              borderPane.setCenter(swingNode);
              borderPane.setTop(bt);

              stage.setScene(new Scene(borderPane, 200, 150));
              stage.show();
          }

          private void createAndSetSwingContent(final SwingNode swingNode) {
              SwingUtilities.invokeLater(new Runnable() {
                  @Override
                  public void run() {
                      JButton bt = new JButton("Click me!");
                      bt.addActionListener(new ActionListener() {
                          @Override
                          public void actionPerformed(ActionEvent e) {
                              System.out.println("Click me...");
                          }
                      });
                      swingNode.setContent(bt);
                  }
              });
          }

          public static void main(String[] args) {
              launch(args);
          }


          class TestComp extends JComponent {
              private String[] strArr;

              TestComp() {
                  this.strArr = new String[1000];
                  for (int i = 0; i < 1000; i++) {
                      strArr[i] = Math.random() + "";
                  }
              }
          }
      }


      if we click the "Add New" button which will set content to null at first ,and then add a class with array.
      we using Eclispe Memory Analysis tool to analysis the heapdump,and find that the variable lwFrame with type JLightweightFrame increase, and force gc cannot release the memory:
      •sun.swing.JLightweightFrame @ 0x921b8e0 - 278,288 (1.56%) bytes.
      •sun.swing.JLightweightFrame @ 0x911bc10 - 278,272 (1.56%) bytes.
      •sun.swing.JLightweightFrame @ 0x915fa88 - 278,208 (1.56%) bytes.
      •sun.swing.JLightweightFrame @ 0x8c64068 - 278,200 (1.56%) bytes.
      •sun.swing.JLightweightFrame @ 0x921bd80 - 278,200 (1.56%) bytes.
      •sun.swing.JLightweightFrame @ 0x921d298 - 278,184 (1.56%) bytes.
      •sun.swing.JLightweightFrame @ 0x921c958 - 278,176 (1.56%) bytes.
      •sun.swing.JLightweightFrame @ 0x8c642b8 - 278,168 (1.56%) bytes.
      •sun.swing.JLightweightFrame @ 0x8d3cef8 - 278,168 (1.56%) bytes.
      •sun.swing.JLightweightFrame @ 0x8c63e18 - 278,160 (1.56%) bytes.
      •sun.swing.JLightweightFrame @ 0x91e3af8 - 278,160 (1.56%) bytes.
      •sun.swing.JLightweightFrame @ 0x8a20c58 - 278,144 (1.56%) bytes.
      •sun.swing.JLightweightFrame @ 0x8c63b80 - 278,144 (1.56%) bytes.
      •sun.swing.JLightweightFrame @ 0x8d3d920 - 278,144 (1.56%) bytes.
      •sun.swing.JLightweightFrame @ 0x8d3dae8 - 278,144 (1.56%) bytes.
      •sun.swing.JLightweightFrame @ 0x921c220 - 278,144 (1.56%) bytes.
      •sun.swing.JLightweightFrame @ 0x921bb30 - 278,136 (1.56%) bytes.
      •sun.swing.JLightweightFrame @ 0x8c63930 - 278,120 (1.56%) bytes.
      •sun.swing.JLightweightFrame @ 0x8a207b8 - 278,112 (1.56%) bytes.
      •sun.swing.JLightweightFrame @ 0x8a20a08 - 278,112 (1.56%) bytes.
      •sun.swing.JLightweightFrame @ 0x921d738 - 278,104 (1.56%) bytes.
      •sun.swing.JLightweightFrame @ 0x882b810 - 278,096 (1.56%) bytes.
      •sun.swing.JLightweightFrame @ 0x8c649a8 - 278,080 (1.56%) bytes.
      •sun.swing.JLightweightFrame @ 0x921cba8 - 278,080 (1.56%) bytes.
       

      how can we reset the SwingNode content rightly?

            anthony Anthony Petrov (Inactive)
            hgreenjfx Herry Green (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported: