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

Memory leak when setting CSS stylesheet on a UI control

XMLWordPrintable

      ADDITIONAL SYSTEM INFORMATION :
      I tested on macOS, but I assume it occurs on other platforms as well.

      A DESCRIPTION OF THE PROBLEM :
      A memory leak occurs when adding and removing new nodes loaded from FXMLLoader. The leak is slow in the attached example, however with more complex fxml (that may include references to other fxml) the leak can be much more substantial. I have seen cases where 3mb are lost on add and remove. It also seems the leak may be related to having a css reference in the fxml.

      STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
      Please run attached example code (it will rapidly flash the window by adding and removing a button). Note that there is java, fxml, and css. After running the attached test for 10 minutes using -Xmx64m I got a OutOfMemoryError.

      EXPECTED VERSUS ACTUAL BEHAVIOR :
      EXPECTED -
      There should be no memory leak when adding and removing a node loaded from FXMLLoader.

      ---------- BEGIN SOURCE ----------
      java
      -----------------------------
      public class MemoryLeak extends Application {

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

          @Override
          public void start(Stage primaryStage) throws Exception {
              AnchorPane pane = new AnchorPane();
              Scene scene = new Scene(pane, 800, 600);
              primaryStage.setScene(scene);
              primaryStage.show();


              new Thread(() -> {
                  try {
                      while (true) {
                          Thread.sleep(50);

                          FXMLLoader loader = new FXMLLoader(getClass()
                                  .getResource("/layout/memory_leak.fxml"));
                          Node node = loader.load();

                          AnchorPane.setBottomAnchor(node, 0.0);
                          AnchorPane.setLeftAnchor(node, 0.0);
                          AnchorPane.setRightAnchor(node, 0.0);
                          AnchorPane.setTopAnchor(node, 0.0);

                          Platform.runLater(() -> pane.getChildren().add(node));
                          Thread.sleep(50);
                          Platform.runLater(() -> pane.getChildren().remove(node));
                      }
                  } catch (Exception e) {
                      System.err.println(e.getMessage());
                  }
              }).start();
          }
      }


      fxml
      -----------------------------
      <?xml version="1.0" encoding="UTF-8"?>

      <?import javafx.scene.control.Button?>
      <Button alignment="CENTER"
              stylesheets="@memory_leak.css"
              xmlns="http://javafx.com/javafx"
              xmlns:fx="http://javafx.com/fxml"
              fx:id="memoryLeak">

      </Button>


      css
      ------------------------------
      #memoryLeak {
          -fx-background-color: #787878;
      }
      ---------- END SOURCE ----------

        1. memory_leak.css
          0.1 kB
        2. memory_leak.fxml
          0.3 kB
        3. MemoryLeak.java
          1 kB
        4. MemoryLeak2.java
          2 kB

            arapte Ambarish Rapte
            webbuggrp Webbug Group
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

              Created:
              Updated: