2013-02-15 48 views
0

下面是一個示例代碼。它載入6個圖像並將它們顯示在屏幕上。每個圖像大小爲2.3 MB。因此,在加載每張圖片時,我會看到每個加載的圖片的內存消耗量上升約3 MB。但事實證明,它爲每個圖像加載10 MB。由圖像造成的高內存消耗

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(); 


} 
} 

在我的應用程序中發現,1.3MB的圖像需要50 MB的空間。有什麼理由?

+0

從你的PNG的BMP應該有近50MB。 – banuj 2013-02-15 08:09:55

回答

6

請記住,PNG圖像的文件大小和內存中未壓縮圖像的內存消耗是非常不同的。

+0

你能否詳細說明一下。如何控制其他因素以減少內存消耗 – Jatin 2013-02-15 08:10:26

+0

減少圖像中的像素數量。 – 2013-02-15 08:16:35