-14

i got heap space error while run a process. My code is given below.Please verify it and give me solution soon.

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();
}
JamesENL
  • 6,400
  • 6
  • 39
  • 64

5 Answers5

1

If you are using Eclipse, here is the full tutorial on how to increase your heap space.

Go to Eclipse Folder

Find Eclipse Icon in Eclipse Folder

Right Click on it you will get option "Show Package Content"

Contents folder will open on screen

If you are on Mac then you'll find "MacOS"

Open MacOS folder you'll find eclipse.ini file

Open it in word or any file editor for edit

...

-XX:MaxPermSize=256m

-Xms40m

-Xmx512m ...

Replace -Xmx512m to -Xmx1024m

Save the file and restart your Eclipse

0

You can increase the max heap size in your VM arguments.

-Xmx=512M

For more clear understanding you can refer this.

Community
  • 1
  • 1
saurav
  • 3,424
  • 1
  • 22
  • 33
0

I strongly recommend you using some csv jars such as opencsv etc. It will makes the code a lot simple and easy to read.

I can't understand your code, since it's too long to read. If the code has no loopholes such as infinite loop etc.

You can try this. add -Xms512m -Xmx1024m -XX:MaxPermSize=512m in eclipse before start to run the program

More info you can find here: http://www.oracle.com/technetwork/java/javase/tech/vmoptions-jsp-140102.html

Seabook
  • 137
  • 3
0

Add a System.out for each row processed, so you can see the progress. You can also add a:

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

and see if it worked for the first set of rows. If your code never gets past row 1, then you know you just have another bug. If you only have 20 rows in your spreadsheet, and you see this goes to row 100, then you know it isn't stopping at what you think is the end.

If you have an enormous spreadsheet (e.g. 100,000 rows), and this happily goes up to 60,000 rows or so before dying, then one of the other answers about increasing heap space would be your best bet.

AngelWarrior
  • 840
  • 7
  • 12
0

Please note that increasing the Java heap size following an java.lang.OutOfMemoryError: Java heap space is quite often just a short term solution.

Yogesh
  • 11
  • 1