Ron*_*nak 8 java memory memory-management javafx-2
我正在创建一个JavaFx应用程序,当我单击一个按钮时,它将以新的方式打开一个表stage.但我的问题是,当我关闭该stage表时,内存不会被应用程序释放.javaFX有什么问题吗?或者我还得做点什么?
我试图在关闭该阶段时将所有内容设置为null,但仍然没有释放内存.
表台上的闭幕事件:
TableView tableView;
舞台myStage;
this.myStage.setOnCloseRequest(new EventHandler<WindowEvent>() {
@Override
public void handle(WindowEvent t) {
TableController.this.myStage.close();
tableView.getItems().clear();
tableView.getColumns().clear();
tableView = null;
TableController.this.myStage = null;
System.gc();
}
});
Run Code Online (Sandbox Code Playgroud)
我创建了一个名为replaceScene的方法来使用fxmlstage for stage 加载场景.它将返回它的控制器并将场景设置到舞台中.
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;
}
}
Run Code Online (Sandbox Code Playgroud)
我首先获取fxml文件的StreamObject(java.io.InputStream),然后将此streamObject传递给FxmlLoader以加载页面,
in = Utility.class.getResourceAsStream(fXml);
我得到的对象sun.net.www.protocol.jar.JarURLConnection $ JarURLInputStream中的 InputStream对象