使用 Spring 引导与 javafx 会消耗大量内存吗?
Does using Spring boot with javafx will consume a lot of memory?
我愿意在我的 JavaFX 应用程序中使用 Spring 引导技术(以利用其依赖注入的优势),但我想知道对内存的影响,如您所知 class 带有“组件”符号的将被加载到 MetaSpace(因为它 Spring 启动将从它创建一个静态对象),因此将数十个 JavaFx 视图控制器加载到 MetaSpace 他们永远不会得到垃圾从应用程序启动到结束收集,这显然是一件坏事,有什么办法可以解决这个问题?
您在评论中写道:
JavaFX applications when the view controller doesn't get garbage collected means also the view objects will always stay there TableViews,ListViews,Panes ... which may take some important space
但我认为不需要那样。
引用 Java 节点的控制器实例与任何其他对象一样只是一个 Java 对象,当 JVM 中不再引用它时,将可用于垃圾回收。
假设您像这样配置 JavaFX Spring启动集成:
因此,您将控制器工厂配置为使用 Spring 个 bean:
fxmlLoader.setControllerFactory(
springContext::getBean
);
那么您的控制器就是 Spring 个范围您可以控制的 bean。
如果你使用prototype scope:
@Bean
@Scope("prototype")
public PersonController personControllerPrototype() {
return new PersonController();
}
则行为如以下所指定:
- Regarding the scope and garbage collection in Spring bean container
Spring does not manage the complete lifecycle of a prototype bean: the container instantiates, configures, and otherwise assembles a prototype object, and hands it to the client, with no further record of that prototype instance.
通常,您将创建一个控制器并添加对它在场景图中实例化的对象树的引用。如果您在导航时替换场景图中的树,并且您没有在任何地方保留对控制器的引用,那么控制器和已从场景图中删除的任何其他关联节点都可以被垃圾收集。
或者,如果您只想加载一次 fxml 并永远保留控制器,您可以使用单例范围来做到这一点。
所以基本上,您可以选择最适合您的应用程序或您正在使用的单个控制器的控制器的范围和生命周期。
我愿意在我的 JavaFX 应用程序中使用 Spring 引导技术(以利用其依赖注入的优势),但我想知道对内存的影响,如您所知 class 带有“组件”符号的将被加载到 MetaSpace(因为它 Spring 启动将从它创建一个静态对象),因此将数十个 JavaFx 视图控制器加载到 MetaSpace 他们永远不会得到垃圾从应用程序启动到结束收集,这显然是一件坏事,有什么办法可以解决这个问题?
您在评论中写道:
JavaFX applications when the view controller doesn't get garbage collected means also the view objects will always stay there TableViews,ListViews,Panes ... which may take some important space
但我认为不需要那样。
引用 Java 节点的控制器实例与任何其他对象一样只是一个 Java 对象,当 JVM 中不再引用它时,将可用于垃圾回收。
假设您像这样配置 JavaFX Spring启动集成:
因此,您将控制器工厂配置为使用 Spring 个 bean:
fxmlLoader.setControllerFactory(
springContext::getBean
);
那么您的控制器就是 Spring 个范围您可以控制的 bean。
如果你使用prototype scope:
@Bean
@Scope("prototype")
public PersonController personControllerPrototype() {
return new PersonController();
}
则行为如以下所指定:
- Regarding the scope and garbage collection in Spring bean container
Spring does not manage the complete lifecycle of a prototype bean: the container instantiates, configures, and otherwise assembles a prototype object, and hands it to the client, with no further record of that prototype instance.
通常,您将创建一个控制器并添加对它在场景图中实例化的对象树的引用。如果您在导航时替换场景图中的树,并且您没有在任何地方保留对控制器的引用,那么控制器和已从场景图中删除的任何其他关联节点都可以被垃圾收集。
或者,如果您只想加载一次 fxml 并永远保留控制器,您可以使用单例范围来做到这一点。
所以基本上,您可以选择最适合您的应用程序或您正在使用的单个控制器的控制器的范围和生命周期。