I have a program which loads many fxml files when executed. The application will be finished in a short time, and loading the application just takes too long.
There are many fxml files (20+) and all these fxml files are loaded with Java code. There will be a point that the application is finished and ready for use, but all files will be loaded with every execution of the program. Can the fxml files only be compiled once, because they won't be changed when finished?
The java code will of course be compiled once, it's only the fxml files. The application now takes 25 seconds to start, with 14 seconds loading the fxml.
Is there a way to make all this faster?
EDIT #1:
Are there any tools that are provided for free and that make the execution of applications (Java) much faster? Or is the execution time merely dependent on the way the program is written?
Which design patterns could help fastening up the execution time of your application?
EDIT #2:
The following code will explain my problem in a nutshell:
package main;
.......
.......
public class MainClass {
....
....
List<InformationController> controllerList;
public mainClass(List<InformationControllers> controllerList) {
this.controllerList = otherClass.getControllerList();
loadFXMLFiles();
}
public void loadFXMLFiles() {
for(InformationController controller : controllerList) {
controller.loadFXML();
}
doOtherStuff();
}
....
}
All those InformationControllers have their loading of other fxml files too, so it's actually a tree of fxml files that is loaded. This is loaded at every start of the application, so is there a way to do this loading only once maybe?
There will be a point that the application is finished and ready for use, but all files will be loaded with every execution of the program. Can the fxml files only be compiled once, because they won't be changed when finished? The java code will of course be compiled once, it's only the fxml files.
Class FXMLLoader. Loads an object hierarchy from an XML document.
The <fx:include> tag can be used to include one fxml file into another. The controller of the included fxml can be injected into the controller of the including file just as any other object created by the FXMLLoader . This is done by adding the fx:id attribute to the <fx:include> element.
These are all great ways to improve performance. However if your issue is that it is running faster in Eclipse and then after compile you see a major slowdown in load times there is a much simpler solution.
For my project I was creating an embedded FX Browser inside an existing swing application. It worked fine in the IDE and loaded almost instantaneously, however upon compiling to a runnable jar, the runnable jar took a very long time to load the FX portion.
The solution to this was during the export to jar to select "Extract required libraries into generated JAR" instead of "Package required libraries into generated JAR". This will significantly speed up load times for any dependencies.
On How to Speed up FXML Performance
I'll make this answer community wiki, if anybody else has further ideas, please edit it and add them.
FXMLLoader.load()
but should instead create an instance of FXMLLoader and reuse that one using the instance loader.load()
method. The reason is that the FXML-Loader caches, e.g. class lookups, and hence is faster on loading the next FXML-File.FXMLLoader.load()
documentation in JavaFX 8 includes a reference to FXML templates, but I don't think that was ever implemented, so maybe it won't help you.Reflection is why FXML is Slow
I think the key reason the Java 8 FXML implementation is slow is that it relies so heavily on reflection and reflection is slow as described in the reflection tutorial:
Because reflection involves types that are dynamically resolved, certain Java virtual machine optimizations can not be performed. Consequently, reflective operations have slower performance than their non-reflective counterparts, and should be avoided in sections of code which are called frequently in performance-sensitive applications.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With