【发布时间】:2016-03-15 02:17:59
【问题描述】:
我在使用javaFX GUI时遇到了一些问题,下面的代码是执行时的性能测试,我在javaFX和swing中创建了三万个按钮对象。当我执行程序时,javaFX写的已经占用了700MB内存,并且随着时间的推移而增加,但是swing写的另一个示例只使用了120MB内存并且没有增加。
这是使用 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) ;
}
那是用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() ;
}
用javaFX编写的程序总是使用swing的5倍或更多,并且当对象大幅增加(swing可以)时,执行窗口无法流畅运行。 有什么办法可以优化吗?
【问题讨论】:
-
请参阅Initial Threads 以使这两个程序更具可比性; profile收集资料供参考。
标签: java performance swing memory javafx