Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JavaFX 2.0 memory leak on creating multiple scenes

I created a sample application that provokes a memory leak.

The problem is that I need to "reload" scenes. If I have two scenes (Bar and Foo) with one button each (ButtonBar and ButtonFoo), the buttons changes the current scene creating a new one. If I stay clicking ButtonBar and ButtonFoo for 5 minutes (or less), the memory consumption of that simple program gets higher and higher.

Bar.java

public class Bar implements Initializable {

@FXML
private Label label;

@FXML
private void toFoo(ActionEvent event) {
    try {
        Button button = (Button) event.getSource();
        Parent root = FXMLLoader.load(getClass().getResource("Foo.fxml"));
        Stage currentStage = (Stage) button.getScene().getWindow();
        currentStage.setScene(new Scene(root));
    } catch (IOException ex) {
        Logger.getLogger(Bar.class.getName()).log(Level.SEVERE, null, ex);
    }
}

@Override
public void initialize(URL url, ResourceBundle rb) {
    // TODO
}    
}

The Foo.java is the same changing de fxml loading.

The fxml contains only one button:

<Button id="buttonBar" layoutX="126" layoutY="90" text="Bar!" onAction="#toFoo" fx:id="buttonBar" />

Is there a real memory-leak problem? Does anyone knows another way to do this? I want that this app stay alive and doing changes forever, like a service. Thanks

like image 456
Javier Avatar asked Oct 24 '22 02:10

Javier


1 Answers

I'm with a similar problem.

When i move the mouse on scene. The memory usage increases and never down.

I believe this is associated with mouse events.

Good Luck!

like image 171
matheusvmbruno Avatar answered Oct 27 '22 10:10

matheusvmbruno