内存不足异常

Posted

技术标签:

【中文标题】内存不足异常【英文标题】:Out of memeory Exception 【发布时间】:2013-07-26 08:13:13 【问题描述】:

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

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

【问题讨论】:

你尝试过使用更大的堆大小?? 我认为这不是获得帮助的好方法“请验证并尽快给我解决方案。” 您是使用给定 API 创建电子表格的人吗? 【参考方案1】:

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

转到 Eclipse 文件夹

在 Eclipse 文件夹中查找 Eclipse 图标

右键单击它,您将获得“显示包内容”选项

Contents 文件夹将在屏幕上打开

如果您使用的是 Mac,那么您会找到“MacOS”

打开 MacOS 文件夹你会发现 eclipse.ini 文件

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

...

-XX:MaxPermSize=256m

-Xms40m

-Xmx512m ...

将 -Xmx512m 替换为 -Xmx1024m

保存文件并重新启动 Eclipse

【讨论】:

【参考方案2】:

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

-Xmx=512M

更清晰的理解可以参考this。

【讨论】:

【参考方案3】:

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

我无法理解您的代码,因为它太长了无法阅读。如果代码没有死循环等漏洞。

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

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

【讨论】:

【参考方案4】:

为处理的每一行添加一个 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 行左右,那么关于增加堆空间的其他答案之一将是您最好的选择。

【讨论】:

【参考方案5】:

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

【讨论】:

以上是关于内存不足异常的主要内容,如果未能解决你的问题,请参考以下文章

内存不足异常 - 非托管内存

为啥我的 C# 应用程序中出现内存不足异常?

Parallel.ForEach 内存不足异常

大位图内存不足异常

内存不足异常

位图 - 内存不足异常