This discussion is archived
4 Replies Latest reply: Mar 1, 2013 5:45 AM by James_D RSS

How to free the memory after closing JavaFX Stage?

982844 Newbie
Currently Being Moderated
I am creating a JavaFx application which contain a button. When I click on that button, it opens a new stage containing a table with thousands of data. It's working fine. But the problem is, when I close the Stage of that table, memory is not getting free by the application i.e. everytime when I open the new stage for table then memory is get increased. Is there any issue with JavaFX? or I have to do something else?

I have tried to set everything null at the time of closing of that stage but still memory is not getting free.

My button click code is :
btn.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent event) {
                Stage stage = new Stage();
                Table1Controller controller = (Table1Controller) Utility.replaceScene("/tablesample/Table1.fxml", stage);
                controller.init(stage);
                stage.setTitle("Sample");
                stage.setWidth(583.0);
                stage.setHeight(485.0);
                stage.initModality(Modality.APPLICATION_MODAL);
                InputStream in = TableSample.class.getResourceAsStream("icon_small.png");
                try {
                    stage.getIcons().add(new Image(in));
                } finally {
                    try {
                        in.close();
                    } catch (IOException ex) {
                    }
                }
                stage.show();
            }
        });
Utility.replacescene method : It loads the scene from given fxml and set to stage. At final It return controller object for that scene.
public static Initializable replaceScene(String fXml, Stage mystage) {
        InputStream in = null;
        try {
            FXMLLoader loader = new FXMLLoader();
            in = Utility.class.getResourceAsStream(fXml);
            loader.setLocation(Utility.class.getResource(fXml));
            loader.setBuilderFactory(new JavaFXBuilderFactory());
            AnchorPane page;
            try {
                page = (AnchorPane) loader.load(in);
            } finally {
                in.close();
            }
            Scene scene = new Scene(page);
            mystage.setScene(scene);
            return loader.getController();
        } catch (Exception ex) {
            return null;
        }
    }
Thanks
  • 1. Re: How to free the memory after closing JavaFX Stage?
    jsmith Guru
    Currently Being Moderated
    Is there any issue with JavaFX? or I have to do something else?
    It's likely an issue with your application code (though it could be a bug in the JavaFX platform too).
    Either way debugging most memory issues in JavaFX (except for ones the graphics card texture related) is the same as debugging them in Java - so just use standard Java profiling tools to try to track down any memory leaks you have.

    Such work is as much an art as a science and takes some experience to get right, so grab your detective cap, download and use the tools linked and try and track it down:
    http://stackoverflow.com/questions/6470651/creating-a-memory-leak-with-java
    http://resources.ej-technologies.com/jprofiler/help/doc/indexRedirect.html?http&&&resources.ej-technologies.com/jprofiler/help/doc/helptopics/memory/memoryLeak.html
    http://www.ej-technologies.com/products/jprofiler/overview.html (recommended tool).
    http://visualvm.java.net/
    http://netbeans.org/kb/articles/nb-profiler-uncoveringleaks_pt1.html
    http://www.ibm.com/developerworks/library/j-leaks/ (this link is really old and somewhat outdated but explains some of the underlying concepts better than some newer articles I found).
  • 2. Re: How to free the memory after closing JavaFX Stage?
    shakir.gusaroff Expert
    Currently Being Moderated
    Hi. If you are using Java 7 you can replace the following:
        InputStream in = TableSample.class.getResourceAsStream("icon_small.png");
        try {
        stage.getIcons().add(new Image(in));
        } finally {
        try {
        in.close();
        } catch (IOException ex) {
        }
        }
    with this:
        try (InputStream in = TableSample.class.getResourceAsStream("icon_small.png")) {
        stage.getIcons().add(new Image(in));
        }
    It may not solve your application's memory problems, but it looks much better.
  • 3. Re: How to free the memory after closing JavaFX Stage?
    csh Journeyer
    Currently Being Moderated
    Do you use Bindings and/or ChangeListener/InvalidationListener?

    In my experience 90% of Memory Leak problems boil down to these two.
  • 4. Re: How to free the memory after closing JavaFX Stage?
    James_D Guru
    Currently Being Moderated
    One other thing to look for is if your FXML file(s) include stylesheets. I believe there is a memory leak associated with stylesheet resources not being released when the node is removed from the scene graph. ([url http://javafx-jira.kenai.com/browse/RT-27896]Bug report).

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points