简体   繁体   中英

50 MB increase in memory consumption on displaying an Image

I have a JavaFX application (Swing users input might also help) which is Image gallery basically. It watches a folder, and as soon as any Image is added, the Image is displayed on the screen.

Sooner as more Images got added, the application memory consumption only grew bigger. On profiling, I have observed that on adding an Image (size 1.3 MB), memory consumption increased by about 50 MB. The class that holds Image is a simple ImageView that holds an Image . Does any one have similar experiences? The behavior is same on Windows & Mac

PS: I know any code here will help, but there is nothing much to be shown. As I said there is a List of ImageView which holds Image. List of ImageView is binded to another List say l1 . On detecting an Image, image is added to l1 and so is added to the actual list which is displayed on screen

EDIT:

I just tried a sample code. I have observed that, on every Iamge it loads (2.3 MB in this case), there is a memory increase of 12 MB each:

package side;

import java.io.File;
import java.io.FileInputStream;
import java.io.InputStream;
import javafx.application.Application;
import javafx.geometry.Orientation;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.control.ScrollBar;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

public class Test extends Application {

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

@Override
public void start(Stage primaryStage) throws Exception {
    ScrollBar bar = new ScrollBar();
    bar.setOrientation(Orientation.VERTICAL);


    final VBox box = new VBox();
    Group root = new Group();
    root.getChildren().addAll(box, bar);
    Scene scene = new Scene(root);
    primaryStage.setScene(scene);
    primaryStage.setTitle("Layout Sample");
    primaryStage.show();


    for (int ik = 0; ik < 6; ik++) {
        System.out.println("1");
        ImageView i = new ImageView();
        InputStream is = new FileInputStream(new File("C:\\Users\\Jatin\\Documents\\BarcodeNew\\w.png"));
        Image im = new Image(is);
        i.setImage(im);
        box.getChildren().add(i);
        is.close();
    }

    //r.close();


}

}

Found two issues:

  1. I wasn't closing my stream.
  2. From this

Remember that the file size of a PNG image and the memory consumption of an uncompressed image in memory are very different.

On specifying the size of the Image it all works well.

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