java.lang.OutOfMemoryError 的问题:Java 堆空间

我正在使用 javaFX 处理 xray 应用程序,并且在加载包含许多图像的考试时出现堆空间错误。

我认为是垃圾收集器的问题,因为我第一次打开考试,一切正常, 但是第二次我犯了这个错误。

当我从考试中调用场景时,我在本地执行(在方法内创建对象)并在调用场景之前调用 System.gc()

现在我不知道如何正确清理加载的图像的哈希图......而且我在加载其他带有许多图像的考试时遇到了这个错误。

我运行 VisualMV,第三条曲线是当我运行并关闭包含许多图像的 que 考试时。

java.lang.OutOfMemoryError 的问题:Java 堆空间

这就是我调用窗口的方式;

private void actionOpenExam() {
    System.gc();
    if (tableViewExames.getSelectionmodel().getSelectedItem() != null) {
        new XRayForm(tableViewExames.getSelectionmodel().getSelectedItem()).display();
        stage.close();
    }
}

方法 display() 我将在此处分批发送,因为以前的开发者使该方法成为一个巨大的方法,而且我还没有重构所有的竞争。

hashMap = new HashMap<>();
for (int i = 0; i < xRayExam.getxRayExamPictureList().size(); i++) {
    XRayExamPicture xRayExamPicture = xRayExam.getxRayExamPictureList().get(i);
    XrayOrientation xRayOrientation = xRayExamPicture.getRegion().getOrientation() == 1 ? XrayOrientation.LANDSCAPE : XrayOrientation.PORTRAIT;
    ThumbnailComponent thumbnailComponent = new ThumbnailComponent(xRayExamPicture.getRegion().getDisposition(),xRayOrientation,xRayExamPicture.getRegion().getX(),xRayExamPicture.getRegion().getY(),xRayExamPicture.getRegion().getDisposition(),thumbnailComponentGroup);

    Image pictureImageFromDisk = loadImageFromDisk(xRayExamPicture.getFilteredFile());
    if (pictureImageFromDisk != null) {
        xRayExamPicture = getImagesFromSelectedExam(xRayExamPicture);//load the image and layers

        //load the image from thumbnail
        Image thumbnailImageFromDisk = loadImageFromDisk(xRayExamPicture.getThumbnailFile());
        if (thumbnailImageFromDisk != null) {
            //if exists set the file
            thumbnailComponent.setThumbnailImage(thumbnailImageFromDisk,false);
        } else {
            //if not exists create a dummy image
            thumbnailComponent.setThumbnailImage(xRayExamPicture.getTempCanvas().getOriginalImage(),true);
        }
    } else {
        xRayExamPicture.setTempCanvas(null);
    }

    thumbnailComponentsList.add(thumbnailComponent);

    thumbnailComponent.setOnmouseclicked(mouseEvent -> {
        if (mouseEvent.getclickCount() == 2) {
            actionImport();
        } else {
            thumbnailComponentGroup.setSelected(thumbnailComponent);
        }
    });

    hashMap.put(thumbnailComponent,xRayExamPicture);
}
congliangfa 回答:java.lang.OutOfMemoryError 的问题:Java 堆空间

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/14896.html

大家都在问