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

Mac: Memory leak when Menu.useSystemMenuBar(true) is used.




      If I use the system MenuBar with OS X the related stages never seen to be GC'd. If I decide not to use the system MenuBar then the whole stage collects as expected.

      Run the provided test class and open 2 windows with each of the top buttons (4 total). Close all the new stages except the primary one. Then click on the "Print References" button and watch the console.

      The console will show that only 2 of the 4 stages were GC'd. As shown in the test case I have tried "turning off" the system MenuBar when the stage is no longer needed and this doesn't have any effect on the memory leak.

      ********************************** Test Class ********************************************
      import java.lang.ref.WeakReference;
      import java.util.HashSet;
      import java.util.Set;

      import javafx.application.Application;
      import javafx.application.Platform;
      import javafx.event.ActionEvent;
      import javafx.event.EventHandler;
      import javafx.geometry.Pos;
      import javafx.scene.Scene;
      import javafx.scene.control.Button;
      import javafx.scene.control.Menu;
      import javafx.scene.control.MenuBar;
      import javafx.scene.control.MenuItem;
      import javafx.scene.layout.BorderPane;
      import javafx.scene.layout.HBox;
      import javafx.scene.layout.Region;
      import javafx.scene.layout.StackPane;
      import javafx.scene.layout.VBox;
      import javafx.stage.Stage;
      import javafx.stage.WindowEvent;

      public class StageTest extends Application {
         @Override public void start(final Stage primaryStage) throws Exception {

            final Set<WeakReference<Object>> refs = new HashSet<>();
            Button showButton = new Button("New View (will collect)...");
            showButton.setOnAction(new EventHandler<ActionEvent>() {
               public void handle(ActionEvent event) {
                  Stage newStage = createNewStage(false);
                  refs.add( new WeakReference<Object>(newStage) );
            Button showButton2 = new Button("New View (won't collect)...");
            showButton2.setOnAction(new EventHandler<ActionEvent>() {
               public void handle(ActionEvent event) {
                  Stage newStage = createNewStage(true);
                  refs.add( new WeakReference<Object>(newStage) );
            Button printButton = new Button("Print References");
            printButton.setOnAction(new EventHandler<ActionEvent>() {
               public void handle(ActionEvent event) {
                  Set<WeakReference<Object>> collected = new HashSet<>();
                  for ( WeakReference<Object> ref: refs ) {
                     if ( ref.get() == null ) {
                  System.out.println(String.format("%d objects collected.",
                  System.out.println(String.format("%d objects remain.",
            VBox box = new VBox(20, new HBox(10, showButton, showButton2), printButton );
            primaryStage.setScene(new Scene( new StackPane(box) ));

         private Stage createNewStage(boolean systemMenubar) {
            final Stage stage = new Stage();
            BorderPane pane = new BorderPane();
            final MenuBar menuBar = new MenuBar();
            Menu menu = new Menu("File");
            menu.getItems().add(new MenuItem("Open..."));
            stage.setScene(new Scene(pane, 800, 600));
            stage.setOnCloseRequest(new EventHandler<WindowEvent>() {
               public void handle(WindowEvent event) {
                  // this line doesn't make any difference to the memory leak...
                  // only collects if I "later" the hide (bug RT-34631)
                  Platform.runLater(new Runnable() {
                     public void run() {
            return stage;
         public static void main(String[] args) throws Exception {





            jgiles Jonathan Giles
            csmithjfx Charles Smith (Inactive)
            0 Vote for this issue
            3 Start watching this issue