简体   繁体   English

JavaFX占用内存并在增加吗?

[英]JavaFX occupy memory and increasing?

I have some problems when i using javaFX GUI,the code below is the performance test when execute, i created thirty thousand button objects in javaFX and swing. 我在使用javaFX GUI时遇到一些问题,下面的代码是执行时的性能测试,我在javaFX中创建了三万个按钮对象并进行摆动。 When I executing the program, written in javaFX already occupied 700MB memory,and its increasing by time,but another sample written in swing only use 120MB memory and not increase. 当我执行程序时,用javaFX编写的程序已经占用了700MB内存,并且随着时间的增加而增加,但是另一个用swing编写的示例仅使用了120MB内存,并且没有增加。

this is the code using javaFX 这是使用javaFX的代码

public class ManyButtons_JavaFX extends Application{
private static final int ROWS = 300;
private static final int COLS = 100;

public void start( Stage stage ){
    stage.setTitle("Many Buttons JavaFX");
    stage.setWidth(600);
    stage.setHeight(400);

    GridPane grid = new GridPane() ;
    grid.setHgap(10);
    grid.setVgap(10) ;

    for( int y = 0 ; y < ROWS ; y++ ){
        for( int x = 0 ; x < COLS ; x ++ ) {
            grid.add(new Button("Button " + x + "," + y ) , x , y ) ;
        }
    }
    ScrollPane scroll = new ScrollPane(grid) ;
    stage.setScene(new Scene(scroll) ) ;
    stage.show() ;
}

public static void main( String[] args ){
    launch(ManyButtons_JavaFX.class , args) ;
}

and that is written in swing 那是写在秋千上

public class ManyButtons_Swing extends JFrame{
private static final int ROWS = 300;
private static final int COLS = 100;

ManyButtons_Swing(){
    this.setTitle("Many Buttons Swing");
    this.setSize(600,400) ;

    JPanel grid = new JPanel(new GridLayout(ROWS , COLS , 10 , 10 )) ;
    for( int y = 0 ; y < ROWS ; y++ ){
        for( int x = 0 ; x < COLS ; x ++ ) {
            grid.add(new JButton("Button " + x + "," + y ) ) ;
        }
    }
    JScrollPane sc = new JScrollPane(grid) ;

    this.setContentPane(sc);
    this.setVisible(true);
}

public static void main( String[] args ){
    new ManyButtons_Swing() ;
}

The program written in javaFX always use five times or more than use swing,and the execute windows cant be running fluent when the object is substantial increased(swing is ok). 用javaFX编写的程序始终使用的频率是使用swing的五倍或更多,并且当对象大大增加(确定可以旋转)时,执行窗口将无法流畅运行。 Do I have any ways to optimize it? 我有什么方法可以对其进行优化吗?

Consider using virtualized controls rather than a node for every data piece 考虑为每个数据块使用虚拟控件而不是节点

30,000 buttons is a lot. 30,000个按钮很多。 In general, I don't advise adding many thousands of nodes to a scene. 通常,我不建议在场景中添加数千个节点。 Instead, I suggest you use a virtualized control that only creates nodes to represent data visible on the screen, rather than all the possible data you have. 相反,我建议您使用虚拟控件,该控件仅创建节点来表示屏幕上可见的数据,而不是您拥有的所有可能数据。 This is how in-built controls such as TableView and ListView work. 这就是内置控件(如TableView和ListView)的工作方式。 They have cell factories and that render dynamic cells that provide a view into the backing data and are continuously updated as the backing data changes (eg as the user scrolls up and down in the ListView). 它们具有单元格工厂,并且呈现动态单元格,这些动态单元格提供了对后备数据的视图,并随着后备数据的变化(例如,当用户在ListView中上下滚动时)不断更新。 Such a strategy allows a TableView to efficiently represent a data structure with hundreds of thousands of items in it. 这种策略允许TableView有效地表示其中包含成千上万项的数据结构。

As you are basing your implementation on a GridPane, a virtualized control that is similar to GridPane functionality is a ControlsFX GridView . 在将实现基于GridPane时,类似于GridPane功能的虚拟控件是ControlsFX GridView I suggest you take a look at that and see if it will fit your need. 我建议您看看它是否适合您的需求。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM