简体   繁体   中英

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.

on both platforms, java 1.7.0 u25 were used.

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. 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. 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.

After glancing at your code, I don't see any leaks, especially since you're not instantiating new TextField instances each time. And yes, it is very, very possible to leak memory in Java. In some ways it's almost more likely because it gives you a false sense of security.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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