如何修复“ java.lang.OutOfMemoryError:Java堆空间”

时间:2019-08-28 12:49:26

标签: java

im制作一个Java .jar应用程序,其中包含图形内容,大部分是全高清图像。目前大约有70 mb的重量。场景更改期间上载的大多数图像。但是有时候,当我尝试下载2-3 MB的图像或.gif时,我得到了“ java.lang.OutOfMemoryError:Java堆空间”。

我知道通过-xmx和-xms可能的解决方案,但这是一个糟糕的解决方案,我应该如何使用图像/图像视图端口来防止此错误?

是否可以将所有图像初始化到CSS样式表中,而不是使用局部功能?

有一个当前函数初始化的例子。

public void Inventory(Stage primaryStage) throws Exception{
    Group root = new Group();
    Scene scene = new Scene(root, x, y, Color.LIGHTBLUE);
    primaryStage.sceneProperty();
    scene.getStylesheets().add("images/style.css");

    Image Inventory = new Image("images/Inventory.jpg");
    ImageView InventoryView = new ImageView(Inventory);
    InventoryView.setFitHeight(y); InventoryView.setFitWidth(x);

    Image WeaponUnpressed = new Image("images/Buttons/Weapons_Unpressed.png");
    ImageView WeaponUnpressedView = new ImageView(WeaponUnpressed);
    Image WeaponPressed = new Image("images/Buttons/Weapons_Pressed.png");
    ImageView WeaponPressedView = new ImageView(WeaponPressed);
    Image WeaponHighlighted = new Image("images/Buttons/Weapons_Highlighted.png");
    ImageView WeaponHighlightedView = new ImageView(WeaponHighlighted);}

大多数通过这种方式初始化的图像。

完整错误消息:

Exception in thread "JavaFX Application Thread" java.lang.OutOfMemoryError: Java heap space
        at java.lang.Object.clone(Native Method)
        at com.sun.javafx.iio.gif.GIFImageLoader2.decodePalette(GIFImageLoader2.java:323)
        at com.sun.javafx.iio.gif.GIFImageLoader2.load(GIFImageLoader2.java:234)
        at com.sun.javafx.iio.ImageStorage.loadAll(ImageStorage.java:364)
        at com.sun.javafx.iio.ImageStorage.loadAll(ImageStorage.java:324)
        at com.sun.javafx.tk.quantum.PrismImageLoader2.loadAll(PrismImageLoader2.java:127)
        at com.sun.javafx.tk.quantum.PrismImageLoader2.<init>(PrismImageLoader2.java:71)
        at com.sun.javafx.tk.quantum.QuantumToolkit.loadImage(QuantumToolkit.java:727)
        at javafx.scene.image.Image.loadImage(Image.java:1065)
        at javafx.scene.image.Image.initialize(Image.java:807)
        at javafx.scene.image.Image.<init>(Image.java:621)
        at com.sun.javafx.css.StyleManager.getCachedImage(StyleManager.java:765)
        at javafx.scene.image.ImageView$2.invalidated(ImageView.java:257)
        at javafx.beans.property.StringPropertyBase.markInvalid(StringPropertyBase.java:109)
        at javafx.beans.property.StringPropertyBase.set(StringPropertyBase.java:144)
        at javafx.css.StyleableStringProperty.set(StyleableStringProperty.java:83)
        at javafx.css.StyleableStringProperty.applyStyle(StyleableStringProperty.java:69)
        at javafx.css.StyleableStringProperty.applyStyle(StyleableStringProperty.java:45)
        at javafx.scene.CssStyleHelper.transitionToState(CssStyleHelper.java:774)
        at javafx.scene.Node.impl_processCSS(Node.java:9189)
        at javafx.scene.Parent.impl_processCSS(Parent.java:1280)
        at javafx.scene.Node.processCSS(Node.java:9056)
        at javafx.scene.Scene.doCSSPass(Scene.java:545)
        at javafx.scene.Scene.preferredSize(Scene.java:1643)
        at javafx.scene.Scene.impl_preferredSize(Scene.java:1720)
        at javafx.stage.Window$SceneModel.invalidated(Window.java:592)
        at javafx.beans.property.ObjectPropertyBase.markInvalid(ObjectPropertyBase.java:111)
        at javafx.beans.property.ObjectPropertyBase.set(ObjectPropertyBase.java:146)
        at javafx.stage.Window.setScene(Window.java:548)
        at javafx.stage.Stage.setScene(Stage.java:252)
        at sample.Main.WorldMap(Main.java:7753)
        at sample.Main$399.handle(Main.java:6512)

0 个答案:

没有答案