Does JavaFX eat my memory?

Before getting upset about the name, I would like to make it clear that I'm better versed in the JavaFX interface. I have been a developer for 9 years using Swing, and currently I decided to try JavaFX. Examples on the net show that JavaFX can indeed create beautiful graphical interfaces compared to Swing. Maybe I'm trying to create and deploy GUIs incorrectly, but one thing is for sure. JavaFX panels load slower than Swing and consume a lot more memory. The same GUI was redesigned using JAVAFX, and it takes up almost 200 MB, and the Swing GUI takes up only 50 MB.

Here I will give a code example of how I create a GUI programmatically using FXML.

public class PanelCreator { private FXMLPane<LoginPaneController> loginFXML; private FXMLPane<RegistrationPaneController> registerFXML; private FXMLPane<EmailValidationPaneController> emailValidationFXML; public PanelCreator() { this.rootPane = rootPane; try { loginFXML = new FXMLPane<LoginPaneController>("Login.fxml"); registerFXML = new FXMLPane<RegistrationPaneController>("Register.fxml"); emailValidationFXML = new FXMLPane<EmailValidationPaneController>("EmailValidation.fxml"); } catch (IOException e) {e.printStackTrace();} // catch } // Constructor Method public Pane getLoginPane() { return loginFXML.getPane(); } // getLoginPane() public Pane getRegisterPane() { return registerFXML.getPane(); } // getRegisterPane public Pane getEmailValidationPane() { return emailValidationFXML.getPane(); } // getEmailValidationPane public LoginPaneController getLoginPaneController() { return loginFXML.getController(); } // getLoginPaneController() public RegistrationPaneController getRegistrationPaneController() { return registerFXML.getController(); } // getRegistrationPaneController() } // class PanelCreator 

The PanelCreator constructor method creates 3 FXMLPane classes, a class that combines both the FXML panel and its controller. The code for the FXMLPane class is shown in the following code.

 public class FXMLPane<T> { private Pane pane; private T paneController; public FXMLPane(String url) throws IOException { URL location = getClass().getResource(url); FXMLLoader fxmlLoader = new FXMLLoader(); fxmlLoader.setLocation(location); fxmlLoader.setBuilderFactory(new JavaFXBuilderFactory()); pane = fxmlLoader.load(location.openStream()); paneController = fxmlLoader.<T>getController(); } // Constructor Method public Pane getPane() { return pane; } // getPane() public T getController() { return paneController; } // getController() } 

Through PanelCreator, I can now use get methods to get each JavaFX panel and its controller, and I don’t need to run the FXML load method every time to get the panel. My current concern is not that creating FXML GUIs is slower than Swing, but moreover, the RAM is 3 times and 4 times larger than the corresponding version of Swing.

Can someone explain to me what I'm doing wrong? FXML files have only basic components in the Grid panel, such as buttons, layers, and text fields.

Memory Consumption Between Java and JavaFX

The code for the above example can be found here.

+8
performance user-interface memory javafx javafx-8
source share
1 answer

To summarize the answers from the comments section:

  • JavaFX requires more memory in general. For example. JavaFX uses double precision for all properties along user interfaces, while Swing uses integer values ​​most of the time. But the difference should not be noticeable.
  • Java consumes more memory as needed. Since Java does not return memory to your system by default, even if you run garbage collection. Thus, if a JavaFX program requires a lot of memory during the initialization process, but then it is freed, the JRE continues to hold the maximum memory level forever (see Figure 1). As a side effect, the GC will fire less often because there is so much free unused memory (see Figure 2). You can change the default value using the JVM -XX option: + UseG1GC. This changes the behavior of distributed memory, its release and the launch of the GC. With this option, the allocated memory should fit better into the used memory. If you want more customization, see Java Heap Tuning.
  • JavaFX is a new structure compared to Swing. Over time, this will be improved in productivity and resource consumption. As you can see in figures 1 and 3, it is already improved. Now it uses 8-9 MB of memory on a 64-bit Linux machine. This is even less memory than the Swing version. I used Oracle Java

     java version "1.8.0_111" Java(TM) SE Runtime Environment (build 1.8.0_111-b14) Java HotSpot(TM) 64-Bit Server VM (build 25.111-b14, mixed mode) 

Memory consumption over time for the JavaFX sample program. It shows a huge amount of free memory compared to the memory used.

Figure 1: Memory consumption over time for the JavaFX sample program. It shows a huge amount of free memory compared to the memory used. The GC was started manually several times to show the used part of the memory without garbage.

Memory consumption over time for the JavaFX sample program, but without manually starting the GC.

Figure 2: Memory consumption over time for the JavaFX sample program, but without manually starting the GC. Used memory grows and grows because GC does not start.

Memory consumption over time for an example JavaFX example using the GC -XX option: + UseG1GC

Figure 3: Memory consumption over time for an example JavaFX example using the GC -XX: + UseG1GC option. After the first GC cycle, the memory size was reduced to fit the actual size of the used memory.

+8
source share

All Articles