0

so basically I have a simple example with a main class that is a stage and when you press a button a new stage will appear over it. If you close it and reopen it over and over eventually the memory that program uses will go up without going back down. Is there a way to get this code to work so that the memory will go back down to the starting point before the button is clicked? Down below are the two classes I used.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;

public class main extends Application{

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

    public void start(Stage stage) throws Exception {

        GridPane grid = new GridPane();
        Button button = new Button("New Screen");
        button.setOnAction(new NewScreen());
        grid.add(button,0,0);
        Scene scene = new Scene(grid,200,200);
        stage.setScene(scene);
        stage.show();
    }
}

and the action class:

import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.layout.GridPane;
import javafx.stage.Stage;


public class NewScreen implements EventHandler<ActionEvent>{

    public void handle(ActionEvent arg0){
        Stage stage = new Stage();
        GridPane grid = new GridPane();
        Scene scene = new Scene(grid,300,300);
        stage.setScene(scene);
        stage.show();
    }
}

Thank you.

Nicolas Filotto
  • 43,537
  • 11
  • 94
  • 122
joe dirt
  • 1
  • 1
  • 2
    Does it actually run out of memory? What actually makes you think there's a memory leak? – James_D Aug 02 '16 at 16:37
  • I believe it's a memory leak because even when the new screen is closed it still shows the memory being used and it never goes back down. Also Say this was a long running program, I would need to free the memory at some point. – joe dirt Aug 02 '16 at 16:47
  • I've just tested there is no memory leak, if you close all the windows that you have created with your button then launch the Garbage collector, you will get the initial heap size – Nicolas Filotto Aug 02 '16 at 16:49
  • Oh thank you Nicolas, do you know why Ubuntu/Windows managers show it using more memory, would it go back down eventually? – joe dirt Aug 02 '16 at 16:50
  • it will go down at the next minor/major GC – Nicolas Filotto Aug 02 '16 at 16:53
  • @joedirt What you're describing is not a memory leak. You are creating new objects and discarding them, so obviously you will use more memory. It's a memory leak if the memory **cannot** be reclaimed, not if the memory doesn't happen to be reclaimed at some particular moment by the GC. – James_D Aug 02 '16 at 16:54
  • @joedirt It seems some other guys had simliar questions, like this one here:http://stackoverflow.com/q/34127284/4170073 and if you search SO, you'll get more like yours: http://stackoverflow.com/search?q=javafx+linux+memory – aw-think Aug 02 '16 at 16:56

0 Answers0