7 Replies Latest reply: Apr 4, 2013 9:36 AM by David Grieve RSS

    Memory not cleared

      I am building a program and noticed the following issue, the memory is not cleared, so everytime i change scenes, the memory usage raises (after some changes it reaches easily to the 300 mbs, around 2-5 mb per change and the program starts with 50mbs usage)

      this is how i create the window:
      public class Main extends Application {
           @Override public void start(Stage stage) throws Exception {
                stage.setScene(new Scene(new CtrlMainMenu(), CtrlWindowOptions.WIDTH, CtrlWindowOptions.HEIGHT));
           public static void main(String[] args) {
      and here how i change scenes
      public class CtrlMainMenu extends AnchorPane{
           @FXML protected AnchorPane main;
           public CtrlMainMenu() throws Exception{
                FXMLLoader fxmlLoader = new FXMLLoader(getClass().getResource("/view/MainMenu.fxml"));
           @FXML protected void NewGame(ActionEvent event) throws Exception{
                Node node = (Node)event.getSource();
                Scene scene = node.getScene();
                scene.setRoot(new CtrlGameOptions());     // tried also with scene.setRoot(new CtrlMainMenu ());, yet stil raises the memory usage
           @FXML protected void Options(ActionEvent event) throws Exception{
                this.getChildren().add(new CtrlOptions());
           @FXML protected void Exit(ActionEvent event){
      do i am doing something wrong? or is it a javafx issue? or the memory is not cleared until it reaches a high amount of mbs?
        • 1. Re: Memory not cleared
          Do you declare a stylesheet in your fxml? There was a bug causing a memory leak when doing this: it appears to be fixed in the latest JavaFX 8 prerelease. See [url https://javafx-jira.kenai.com/browse/RT-27896]RT-27896 on JIRA.
          • 2. Re: Memory not cleared
            here an example of the fxml used
            <?xml version="1.0" encoding="UTF-8"?>
            <?import javafx.scene.control.*?>
            <?import javafx.scene.layout.*?>
            <?import javafx.scene.text.*?>
            <?import javafx.scene.*?>
            <fx:root type="AnchorPane" xmlns:fx="http://javafx.com/fxml"
                     stylesheets="view/Style.css" id="mainBackgorund">
                      <GridPane xmlns:fx="http://javafx.com/fxml" alignment="center"
                                AnchorPane.topAnchor="0.0" AnchorPane.bottomAnchor="128.0"
                                AnchorPane.leftAnchor="0.0" AnchorPane.rightAnchor="0.0"
                                hgap="10" vgap="10">
                           <Text text="Title" GridPane.columnIndex="0" GridPane.rowIndex="0" id="title" />
                           <VBox spacing="8" GridPane.columnIndex="0" GridPane.rowIndex="1">
                                <Button text="New Game" onAction="#NewGame"/>
                                <Button text="Options" onAction="#Options"/>
                                <Button text="Exit" onAction="#Exit"/>
            • 3. Re: Memory not cleared
              So, yes, you declare a stylesheet in the fxml. Which version of JavaFX are you running? If you haven't already, try under the latest FX8 prerelease, and see if it displays the same behavior.

              If that fixes it, and it's not a showstopper (i.e. you're not actually running out of memory under reasonable use on the machines you're targeting), I'd just wait and require FX8 as soon as you can. If not, you'll need to find a workaround to avoid declaring stylesheets in the fxml files that are used to dynamically add and remove nodes from the scene graph.
              • 4. Re: Memory not cleared
                I am using the latest JDK (since had some issues and had to reinstall, so ended up using it) SE 7u17

                memory is not currently a problem, since it raises quite slowly (and the program is for my university project) and doubt i will change scenes like 100 times just to raise the memory usage by 300-500mbs, still, dont like the idea that this memory is not cleared up

                so to "solve" this problem, i should remove the line stylesheets="view/Style.css" and load the .css on the constructor?

                EDIT: or maybe i could save all the already loaded resources and if it has been already loaded, isntead of creating again, just retrieve it...

                Edited by: Hello World! on 03-abr-2013 15:36
                • 5. Re: Memory not cleared
                  David Grieve
                  You should try javafx version 8, as James recommended. This is a bug in 2.2.x which has been fixed in 8. If you are using JavaFX co-bundled with SE 7u17, you are running a 2.2 version of JavaFX (2.2.7, I think).

                  Another way to get around this issue would be to replace the root of the scene (i.e., scene.setRoot(newRoot)) rather than the scene itself.
                  • 6. Re: Memory not cleared
                    Another way to get around this issue would be to replace the root of the scene (i.e., scene.setRoot(newRoot)) rather than the scene itself.
                    Does this work around the bug, if you leave the stylesheet applied to the root? The original test case I created ({message:id=10812643}) replaced the center of a BorderPane and showed the same behavior. The only way I found around this was to load all the required css once into the Scene, and have no stylesheets applied to the dynamically added nodes.
                    • 7. Re: Memory not cleared
                      David Grieve
                      Yes, just add stylesheets to the Scene, not to the nodes.

                      Although, you might try removing all of the stylesheets from the nodes before switching out the scene or the root.