简体   繁体   English

Windows中的JavaFx内存泄漏,但Mac OSX中没有

[英]JavaFx memory leak in windows but not mac osx

i have the following javafx code when executed with -Xmx10m jvm option, it runs to completion after clicking on the button (it adds and removes 250 TextFields 100000 times) on mac osx but it runs out of memory on windows 7. 使用-Xmx10m jvm选项执行时,我具有以下javafx代码,在Mac OS X上单击按钮(它会添加和删除250个TextField 100000次)后运行完成,但在Windows 7上内存不足。

on both platforms, java 1.7.0 u25 were used. 在两个平台上,均使用Java 1.7.0 u25。

import java.util.ArrayList;
import java.util.List;

import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class SimpleTextFieldTest extends Application {

    private List<TextField> list = new ArrayList<TextField>();
    private Label message = new Label();

    private void init(Stage primaryStage) {

        System.out.println("Start Testing");
        for (int i = 0; i < 250; i++) {
            TextField textField = new TextField();
            textField.setPrefWidth(100);
            textField.setText("hello");
            list.add(textField);
        }
        System.out.println("end of initial textBox");

        final VBox root = new VBox();
        primaryStage.setScene(new Scene(root, 200, 200));

        Button button1 = new Button();
        button1.setText("Start");
        button1.setOnAction(new EventHandler<ActionEvent>() {
            @Override
            public void handle(ActionEvent arg0) {

                try{
                    for (int i = 0; i < 100000; i++) {

                        for(TextField text : list){
                            root.getChildren().add(text);
                        }

                        root.getChildren().removeAll(list);
                    }
                }catch(Exception ex){
                    ex.printStackTrace();
                }

                System.out.println("end of Test");
            }
        });
        root.getChildren().add(button1);
        root.getChildren().add(message);
    }

    @Override
    public void start(Stage primaryStage) throws Exception {
        init(primaryStage);
        primaryStage.show();
    }

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

Running out of memory does not always imply a memory leak. 内存不足并不总是意味着内存泄漏。 It may simply mean you don't have enough memory to do what you're trying to do. 这可能只是意味着您没有足够的内存来执行您要尝试执行的操作。 10 megs isn't much at all. 10兆根本不算什么。 It's likely that the in-memory representations of JavaFX nodes differ somewhat between Mac and Windows and perhaps the Windows representation requires a bit more space, or the base consumption of the memory pool is higher to start with on Windows. 在Mac和Windows之间,JavaFX节点的内存表示形式可能有所不同,并且Windows表示形式可能需要更多空间,或者从Windows开始,内存池的基本消耗会更高。 Bottom line: it's not realistic to expect that exact same memory usage across platforms. 底线:跨平台使用完全相同的内存使用是不现实的。 Also, the implementation of the -Xmx option itself may even differ. 同样,-Xmx选项本身的实现甚至可能不同。

After glancing at your code, I don't see any leaks, especially since you're not instantiating new TextField instances each time. 看完您的代码后,我看不到任何泄漏,尤其是因为您没有每次都实例化新的TextField实例。 And yes, it is very, very possible to leak memory in Java. 是的,在Java中非常有可能泄漏内存。 In some ways it's almost more likely because it gives you a false sense of security. 在某些方面,它更有可能是因为它给您带来了错误的安全感。

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

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