内存不足异常

时间:2013-07-26 07:00:21

标签: java memory apache-poi core

运行进程时出现堆空间错误。我的代码如下。请验证并尽快给我解决。

public static void Read_File(String Filename)
        throws IOException {
    FileInputStream myInput = null;

    myInput = new FileInputStream(Filename);
    POIFSFileSystem myFileSystem = new POIFSFileSystem(myInput);
    HSSFWorkbook myWorkBook = new HSSFWorkbook(myFileSystem);
    HSSFSheet mySheet = myWorkBook.getSheetAt(0);

    int RowCount = 0;
    for (Row row : mySheet) {
        if (RowCount >= 1) {
            Cell Eincell = row.getCell(0);
            Cell Namecell = row.getCell(1);
            Cell InCareofNamecell = row.getCell(2);
            Cell Addresscell = row.getCell(3);
            Cell Citycell = row.getCell(4);
            Cell Statecell = row.getCell(5);
            Cell ZipCodecell = row.getCell(6);
            Cell GroupExemptionNumbercell = row.getCell(7);
            Cell SubsectionCodecell = row.getCell(8);
            Cell AffiliationCodecell = row.getCell(9);
            Cell ClassificationCodecell = row.getCell(10);
            Cell RulingDatecell = row.getCell(11);
            Cell DeductibilityCodecell = row.getCell(12);
            Cell FoundationCodcell = row.getCell(13);
            Cell ActivityCodecell = row.getCell(14);
            Cell OrganizationCodecell = row.getCell(15);
            Cell ExemptOrganizationStatusCodecell = row.getCell(16);
            Cell AdvancedRulingExpirationDatecell = row.getCell(17);
            Cell TaxPeriodcell = row.getCell(18);
            Cell AssetCodecell = row.getCell(19);
            Cell IncomeCodecell = row.getCell(20);
            Cell FilingRequirementCodecell = row.getCell(21);
            Cell PFFilingRequirementCodecell = row.getCell(22);
            Cell Blankcell = row.getCell(23);
            Cell AccountingPeriodcell = row.getCell(24);
            Cell AssetAmountcell = row.getCell(25);
            Cell IncomeAmountcell = row.getCell(26);
            Cell NegativeSigncell = row.getCell(27);
            Cell Form90RevenueAmountcell = row.getCell(28);
            Cell NegativeSign1cell = row.getCell(29);
            Cell NTEECodecell = row.getCell(30);
            Cell SortorSecondaryNamecell = row.getCell(31);

            if (Eincell == null) {
                Einvalue = 0;
            } else {
                try {
                    String ass = String.valueOf(Eincell.getStringCellValue());
                    Einvalue = Integer.parseInt(ass);
                } catch (Exception ex) {
                    Einvalue = 0;
                }
            }

            if (Namecell == null) {
                namevalue = "0";
            } else {
                try {
                    namevalue = Namecell.getStringCellValue();
                } catch (Exception ex1) {
                    namevalue = "0";
                }
            }

            if (InCareofNamecell == null) {
                incarevalue = "0";
            } else {
                try {
                    incarevalue = InCareofNamecell.getStringCellValue();
                } catch (Exception ex1) {
                    incarevalue = "0";
                }
            }
            if (Addresscell == null) {
                addressvalue = "0";
            } else {
                try {
                    addressvalue = Addresscell.getStringCellValue();
                } catch (Exception ex1) {
                    addressvalue = "0";
                }
            }
            if (Citycell == null) {
                cityvalue = "0";
            } else {
                try {
                    cityvalue = Citycell.getStringCellValue();
                } catch (Exception ex1) {
                    cityvalue = "0";
                }
            }
            if (Statecell == null) {
                Statevalue = "0";
            } else {
                try {
                    Statevalue = Statecell.getStringCellValue();
                } catch (Exception ex1) {
                    Statevalue = "0";
                }
            }

            if (ZipCodecell == null) {
                zipvalue = "0";
            } else {
                try {
                    zipvalue = ZipCodecell.getStringCellValue();
                } catch (Exception ex1) {
                    zipvalue = "0";
                }
            }
            if (GroupExemptionNumbercell == null) {
                groupvalue = 0;
            } else {
                try {
                    groupvalue = (int) GroupExemptionNumbercell.getNumericCellValue();
                } catch (Exception ex) {
                    groupvalue = 0;
                }
            }

            if (SubsectionCodecell == null) {
                SubsectionCodevalue = 0;
            } else {
                try {
                    SubsectionCodevalue = (int) SubsectionCodecell.getNumericCellValue();
                } catch (Exception ex) {
                    SubsectionCodevalue = 0;
                }
            }
            if (AffiliationCodecell == null) {
                AffiliationCodevalue = 0;
            } else {
                try {
                    AffiliationCodevalue = (int) AffiliationCodecell.getNumericCellValue();
                } catch (Exception ex) {
                    AffiliationCodevalue = 0;
                }
            }
            if (ClassificationCodecell == null) {
                ClassificationCodevalue = 0;
            } else {
                try {
                    ClassificationCodevalue = (int) ClassificationCodecell.getNumericCellValue();
                } catch (Exception ex) {
                    ClassificationCodevalue = 0;
                }
            }
            if (RulingDatecell == null) {
                RulingDatevalue = 0;
            } else {
                try {
                    RulingDatevalue = (int) RulingDatecell.getNumericCellValue();
                } catch (Exception ex) {
                    RulingDatevalue = 0;
                }
            }

            if (DeductibilityCodecell == null) {
                DeductibilityCodevalue = 0;
            } else {
                try {
                    DeductibilityCodevalue = (int) DeductibilityCodecell.getNumericCellValue();
                } catch (Exception ex) {
                    DeductibilityCodevalue = 0;
                }
            }
            if (FoundationCodcell == null) {
                Foundationcodevalue = 0;
            } else {
                try {
                    Foundationcodevalue = (int) FoundationCodcell.getNumericCellValue();
                } catch (Exception ex) {
                    Foundationcodevalue = 0;
                }
            }

            if (ActivityCodecell == null) {
                ActivityCodevalue = 0;
            } else {
                try {
                    ActivityCodevalue = (int) ActivityCodecell.getNumericCellValue();
                } catch (Exception ex) {
                    ActivityCodevalue = 0;
                }
            }
            if (OrganizationCodecell == null) {
                OrganizationCodevalue = 0;
            } else {
                try {
                    OrganizationCodevalue = (int) OrganizationCodecell.getNumericCellValue();
                } catch (Exception ex) {
                    OrganizationCodevalue = 0;
                }
            }

            if (ExemptOrganizationStatusCodecell == null) {
                ExemptOrganizationStatusCodevalue = 0;
            } else {
                try {
                    ExemptOrganizationStatusCodevalue = (int) ExemptOrganizationStatusCodecell.getNumericCellValue();
                } catch (Exception ex) {
                    ExemptOrganizationStatusCodevalue = 0;
                }
            }
            if (AdvancedRulingExpirationDatecell == null) {
                AdvancedRulingExpirationDatevalue = 0;
            } else {
                try {
                    AdvancedRulingExpirationDatevalue = (int) AdvancedRulingExpirationDatecell.getNumericCellValue();
                } catch (Exception ex) {
                    AdvancedRulingExpirationDatevalue = 0;
                }
            }
            if (TaxPeriodcell == null) {
                TaxPeriodvalue = 0;
            } else {
                try {
                    TaxPeriodvalue = (int) TaxPeriodcell.getNumericCellValue();
                } catch (Exception ex) {
                    TaxPeriodvalue = 0;
                }
            }

            if (AssetCodecell == null) {
                AssetCodevalue = 0;
            } else {
                try {
                    AssetCodevalue = (int) AssetCodecell.getNumericCellValue();
                } catch (Exception ex) {
                    AssetCodevalue = 0;
                }
            }
            if (IncomeCodecell == null) {
                IncomeCodevalue = 0;
            } else {
                try {
                    IncomeCodevalue = (int) IncomeCodecell.getNumericCellValue();
                } catch (Exception ex) {
                    IncomeCodevalue = 0;
                }
            }

            if (FilingRequirementCodecell == null) {
                FilingRequirementCode = 0;
            } else {
                try {
                    FilingRequirementCode = (int) FilingRequirementCodecell.getNumericCellValue();
                } catch (Exception ex) {
                    FilingRequirementCode = 0;
                }
            }

            if (PFFilingRequirementCodecell == null) {
                PFFilingRequirementCodevalue = 0;
            } else {
                try {
                    PFFilingRequirementCodevalue = (int) PFFilingRequirementCodecell.getNumericCellValue();
                } catch (Exception ex) {
                    PFFilingRequirementCodevalue = 0;
                }
            }
            try {
                Blankvalue = Blankcell.getStringCellValue();
            } catch (Exception ex) {
                Blankvalue = "0";
            }
            try {
                AccountingPeriodvalue = (int) AccountingPeriodcell.getNumericCellValue();
            } catch (Exception ex) {
                AccountingPeriodvalue = 0;
            }
            try {
                AssetAmountvalue = AssetAmountcell.getNumericCellValue();
            } catch (Exception ex) {
                AssetAmountvalue = 0.0D;
            }
            try {
                IncomeAmountvalue = IncomeAmountcell.getNumericCellValue();
            } catch (Exception ex) {
                IncomeAmountvalue = 0.0D;
            }
            try {
                navigaresign = NegativeSigncell.getStringCellValue();
            } catch (Exception ex) {
                navigaresign = "0";
            }
            try {
                Form990RevenueAmountvalue = Form90RevenueAmountcell.getNumericCellValue();
            } catch (Exception ex) {
                Form990RevenueAmountvalue = 0.0D;
            }
            try {
                navigaresign1_value = NegativeSign1cell.getStringCellValue();
            } catch (Exception ex) {
                navigaresign1_value = "0";
            }

            try {
                NTEE_Code_value = NTEECodecell.getStringCellValue();
            } catch (Exception ex) {
                NTEE_Code_value = "0";
            }
            try {
                SortorSecondaryName_value = SortorSecondaryNamecell.getStringCellValue();
            } catch (Exception ex) {
                SortorSecondaryName_value = "0";
            }
            try {
                YearValue = String.valueOf(TaxPeriodvalue).substring(0, 4).trim();
            } catch (Exception ex) {
                YearValue = "0";
            }
            try {
                RangeValue = Math.ceil(Form990RevenueAmountvalue);
            } catch (Exception ex) {
                RangeValue = 0.0D;
            }

            if (RangeValue < 1000000.0D) {
                ResultRange = "Less than $1 Million";
            } else if ((RangeValue > 1000000.0D) && (RangeValue < 9999999.0D)) {
                ResultRange = " $1-9.99 Million";
            } else if ((RangeValue > 10000000.0D) && (RangeValue < 24999999.0D)) {
                ResultRange = "  $10-24.99 Million";
            } else if ((RangeValue > 25000000.0D) && (RangeValue < 49999999.0D)) {
                ResultRange = "  $25-49.99 Million";
            } else if ((RangeValue > 50000000.0D) && (RangeValue < 74999999.0D)) {
                ResultRange = "  $50-74.99 Million";
            } else if ((RangeValue > 100000000.0D) && (RangeValue < 124999999.0D)) {
                ResultRange = "  $100-124.99 Million";
            } else if ((RangeValue > 125000000.0D) && (RangeValue < 149999999.0D)) {
                ResultRange = " $125-149.99 Million";
            } else if ((RangeValue > 150000000.0D) && (RangeValue < 199999999.0D)) {
                ResultRange = "  $150-199.99 Million";
            } else if ((RangeValue > 200000000.0D) && (RangeValue < 249999999.0D)) {
                ResultRange = "  $200-249.99 Million";
            } else if ((RangeValue > 250000000.0D) && (RangeValue < 299999999.0D)) {
                ResultRange = "  $250-299.99 Million";
            } else if ((RangeValue > 300000000.0D) && (RangeValue < 349999999.0D)) {
                ResultRange = "  $300-349.99 Million";
            } else if ((RangeValue > 350000000.0D) && (RangeValue < 399999999.0D)) {
                ResultRange = "  $350-399.99 Million";
            } else if ((RangeValue > 400000000.0D) && (RangeValue < 449999999.0D)) {
                ResultRange = "   $400-449.99 Million";
            } else if ((RangeValue > 450000000.0D) && (RangeValue < 499999999.0D)) {
                ResultRange = "  $450-499.99 Million";
            } else if ((RangeValue > 500000000.0D) && (RangeValue < 549999999.0D)) {
                ResultRange = "  $500-549.99 Million";
            } else if ((RangeValue > 550000000.0D) && (RangeValue < 599999999.0D)) {
                ResultRange = " $550-599.99 Million";
            } else if ((RangeValue > 600000000.0D) && (RangeValue < 649999999.0D)) {
                ResultRange = "$600-649.99 Million";
            } else if ((RangeValue > 650000000.0D) && (RangeValue < 699999999.0D)) {
                ResultRange = "  $650-699.99 Million";
            } else if ((RangeValue > 700000000.0D) && (RangeValue < 749999999.0D)) {
                ResultRange = "$700-749.99 Million";
            } else if ((RangeValue > 750000000.0D) && (RangeValue < 799999999.0D)) {
                ResultRange = "$750-799.99 Million";
            } else if ((RangeValue > 800000000.0D) && (RangeValue < 899999999.0D)) {
                ResultRange = "$800-899.99 Million";
            } else if ((RangeValue > 900000000.0D) && (RangeValue < 999999999.0D)) {
                ResultRange = "$900-999.99";
            } else if ((RangeValue > 1000000000.0D) && (RangeValue < 4999999999.0D)) {
                ResultRange = "$1-4.99 Billion";
            } else if ((RangeValue > 5000000000.0D) && (RangeValue < 14999999999.0D)) {
                ResultRange = "$5-14.99 Billion";
            } else if ((RangeValue > 15000000000.0D) && (RangeValue < 24999999999.0D)) {
                ResultRange = "$15-24.99 Billion";
            } else if ((RangeValue > 25000000000.0D) && (RangeValue < 49999999999.0D)) {
                ResultRange = "$25-49.99 Billion";
            } else if ((RangeValue > 50000000000.0D) && (RangeValue < 74999999999.0D)) {
                ResultRange = "$50-74.99 Billion";
            } else if ((RangeValue > 75000000000.0D) && (RangeValue < 99999999999.0D)) {
                ResultRange = "$75-99.99 Billion";
            } else if ((RangeValue > 100000000000.0D) && (RangeValue < 149999999999.0D)) {
                ResultRange = "$100-149.99 Billion";
            } else if ((RangeValue > 150000000000.0D) && (RangeValue < 199999999999.0D)) {
                ResultRange = "$150-199.99 Billion";
            } else if ((RangeValue > 200000000000.0D) && (RangeValue < 249999999999.0D)) {
                ResultRange = "$200-249.99 Billion";
            } else if ((RangeValue > 250000000000.0D) && (RangeValue < 299999999999.0D)) {
                ResultRange = " $250-299.99 Billion";
            } else if ((RangeValue > 300000000000.0D) && (RangeValue < 349999999999.0D)) {
                ResultRange = " $300-349.99 Billion";
            } else if ((RangeValue > 350000000000.0D) && (RangeValue < 399999999999.0D)) {
                ResultRange = "    $350-399.99 Billion";
            } else if ((RangeValue > 400000000000.0D) && (RangeValue < 449999999999.0D)) {
                ResultRange = " $400-449.99 Billion";
            } else if ((RangeValue > 450000000000.0D) && (RangeValue < 499999999999.0D)) {
                ResultRange = "  $450-499.99 Billion";
            } else if (RangeValue > 500000000000.0D) {
                ResultRange = "  $500 Billion +";
            }

            table.addRow(new Object[]{Integer.valueOf(Einvalue), namevalue, incarevalue, addressvalue, cityvalue, Statevalue, zipvalue, Integer.valueOf(groupvalue), Integer.valueOf(SubsectionCodevalue), Integer.valueOf(AffiliationCodevalue), Integer.valueOf(ClassificationCodevalue), Integer.valueOf(RulingDatevalue), Integer.valueOf(DeductibilityCodevalue), Integer.valueOf(Foundationcodevalue), Integer.valueOf(ActivityCodevalue), Integer.valueOf(OrganizationCodevalue), Integer.valueOf(ExemptOrganizationStatusCodevalue), Integer.valueOf(AdvancedRulingExpirationDatevalue), Integer.valueOf(TaxPeriodvalue), Integer.valueOf(AssetCodevalue), Integer.valueOf(IncomeCodevalue), Integer.valueOf(FilingRequirementCode), Integer.valueOf(PFFilingRequirementCodevalue), Blankvalue, Integer.valueOf(AccountingPeriodvalue), Double.valueOf(AssetAmountvalue), Double.valueOf(IncomeAmountvalue), navigaresign, Double.valueOf(Form990RevenueAmountvalue), navigaresign1_value, NTEE_Code_value, SortorSecondaryName_value, YearValue, ResultRange});
        }

        System.gc();

        RowCount++;
    }

    System.gc();
    myInput.close();
}

5 个答案:

答案 0 :(得分:1)

如果您使用的是Eclipse,here是关于如何增加堆空间的完整教程。

  

转到Eclipse文件夹

     

在Eclipse文件夹中找到Eclipse图标

     

右键单击它,您将获得选项&#34;显示包装内容&#34;

     

目录文件夹将在屏幕上打开

     

如果你在Mac上,那么你会发现&#34; MacOS&#34;

     

打开MacOS文件夹,您将找到eclipse.ini文件

     

在word或任何文件编辑器中打开它进行编辑

     

...

     

-XX:MaxPermSize参数=256米

     

-Xms40m

     

-Xmx512m ......

     

将-Xmx512m替换为-Xmx1024m

     

保存文件并重启Eclipse

答案 1 :(得分:0)

您可以增加VM参数中的最大堆大小。

-Xmx = 512M

为了更清楚地理解,您可以参考this

答案 2 :(得分:0)

我强烈建议你使用一些csv jar,比如opencsv等。它会让代码变得简单易读。

我无法理解您的代码,因为阅读时间太长。如果代码没有漏洞,例如无限循环等。

你可以试试这个。 在开始运行程序之前在eclipse中添加-Xms512m -Xmx1024m -XX:MaxPermSize = 512m

您可以在此处找到更多信息:http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html

答案 3 :(得分:0)

为处理的每一行添加System.out,以便您可以查看进度。您还可以添加:

if(RowCount%10==0){
    System.out.println(RowCount);
}
if(RowCount > 100){
    return;
}
//tweak numbers per your needs/expectations

并查看它是否适用于第一组行。如果您的代码永远不会超过第1行,那么您就知道自己只有另一个错误。如果您的电子表格中只有20行,并且您看到第100行,那么您知道它并没有停留在您认为结束的位置。

如果您有一个巨大的电子表格(例如100,000行),并且在死亡之前这个快乐地达到60,000行,那么关于增加堆空间的其他答案之一将是您最好的选择。

答案 4 :(得分:0)

请注意,在java.lang.OutOfMemoryError: Java heap space之后增加Java堆大小通常只是一个短期解决方案。