如何防止我的java程序内存不足?

时间:2016-02-01 03:44:56

标签: java memory arraylist

我正在编写一个java程序来扫描具有数万个高清图像的文件夹,并从中提取一些元数据。我已经让程序运行得很好,除了它只能扫描包含大约100个图像的文件夹,然后才会出现内存不足错误。虽然我可能能够增加最大内存分配(尚未真正研究过它),但这种解决方案并不理想,因为我需要大约10 GB的内存才能运行该程序,按我的计算。

我对如何重写以下代码段感兴趣,以便它在运行时不会耗尽尽可能多的内存。

    ArrayList<File> imageFiles = new ArrayList<File>(); 

    ...

    // Create an ArrayList of image sets, and add the image files to those sets, but 
    // don't convert them to BufferedImage objects until the sets themselves are processing.
    ArrayList<ImageSet> groupedFiles = new ArrayList<ImageSet>();
    for (File image : imageFiles) {
        int caseNumber = Ops.getCaseNumberFromFilename(image.getName());
        boolean foundMatchingCase = false;
        for (ImageSet i : groupedFiles) {
            if (i.caseNumber == caseNumber) {
                foundMatchingCase = true;
                i.addImageToSet(image);
            }
        }
        if (!foundMatchingCase) {
            Log.write("Creating new set for image " + image.getName(),
                    Log.STANDARD);
            ImageSet newSet = new ImageSet(caseNumber);
            newSet.addImageToSet(image);
            groupedFiles.add(newSet);
        }
    }

如果需要其他上下文,the source for the entire program is on my github。 (上面的代码来自src / threads / ImageProcessingThreadA.java。)

感谢你们提出的任何想法。

0 个答案:

没有答案
相关问题