JavaFX - loading images and memory issues

I have a problem uploading images to my application. I am trying to make a simple image browser. On the left, I have a list of folders. After clicking on the folder name in the list of images should appear on the right (flow panel). Each image is in a HBox with a frame. But I quickly get this error:

java.lang.outofmemoryerror java heap space

I looked at the task manager - if you upload only 6 photos, the application takes up 500 MB of memory! And one more thing - if I change the folder in the list, the memory will remain taken. When I select a different folder, I create a new FlowPane

flowPane = new FlowPane();

So the old one with all ImageViews should be deleted by the garbage collector, right?

How to effectively manage images in the application?

  for(int i = 0 ; i < zdjecia.length; i++){
        ImageView iv2 = new ImageView();
        Image image = new Image("file:"+zdjecia[i].getAbsolutePath());
         iv2.setImage(image);
         if( image.getHeight() > image.getWidth()){
                iv2.fitHeightProperty().bind(szerokoscZdjecia.multiply(0.8).subtract(6));
        }else
                iv2.fitWidthProperty().bind(szerokoscZdjecia.subtract(6));

         iv2.setPreserveRatio(true);
         iv2.setSmooth(true);
         iv2.setCache(false);

         String styl = "-fx-border-color: #b2b3b3;"
                 + "-fx-border-width: 2;";

         HBox boxNaFotke = new HBox();
         boxNaFotke.prefWidthProperty().bind(szerokoscZdjecia);
         boxNaFotke.prefHeightProperty().bind(szerokoscZdjecia.multiply(0.8));
         boxNaFotke.setAlignment(Pos.CENTER);
         boxNaFotke.setStyle(styl);
         boxNaFotke.getChildren().add(iv2);
         fotki.add(boxNaFotke);
         flowPane.getChildren().add(boxNaFotke);
    }
+5
source share
2

Image , .

:

// 100x150
// my.res Image image2 = new Image("my/res/flower.png", 100, 150, false, false);

// 100, // ,
// URL- http-
Image image3 = new Image("http://sample.com/res/flower.png", 100, 0, false, false);

+3

JavaFX, setStyle. , , StyleHelper , setStyle ( HashMap GC root com.sun.javafx.css.StyleManager $Holder). , .

boxNaFotke.setStyle(styl); , .

0

All Articles