title: Excel sheet POI validation: Out Of Memory Error
body: <p>I am trying to validate an Excel file using Java before dumping it to database.</p>
  <p>Here is my code snippet which causes error.</p>
  <pre><code>try {
      fis = new FileInputStream(file);
      wb = new XSSFWorkbook(fis);
      XSSFSheet sh = wb.getSheet(&quot;Sheet1&quot;);
      for(int i = 0 ; i &lt; 44 ; i++){
          XSSFCell a1 = sh.getRow(1).getCell(i);
          printXSSFCellType(a1);
      }
      
  } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  }
  catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
  }
  </code></pre>
  <p>Here is the error I get</p>
  <pre><code>Exception in thread &quot;main&quot; java.lang.OutOfMemoryError: Java heap space
      at java.util.ArrayList.&lt;init&gt;(Unknown Source)
      at java.util.ArrayList.&lt;init&gt;(Unknown Source)
      at org.apache.xmlbeans.impl.values.NamespaceContext$NamespaceContextStack.&lt;init&gt;(NamespaceContext.java:78)
      at org.apache.xmlbeans.impl.values.NamespaceContext$NamespaceContextStack.&lt;init&gt;(NamespaceContext.java:75)
      at org.apache.xmlbeans.impl.values.NamespaceContext.getNamespaceContextStack(NamespaceContext.java:98)
      at org.apache.xmlbeans.impl.values.NamespaceContext.push(NamespaceContext.java:106)
      at org.apache.xmlbeans.impl.values.XmlObjectBase.check_dated(XmlObjectBase.java:1273)
      at org.apache.xmlbeans.impl.values.XmlObjectBase.stringValue(XmlObjectBase.java:1484)
      at org.apache.xmlbeans.impl.values.XmlObjectBase.getStringValue(XmlObjectBase.java:1492)
      at org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTCellImpl.getR(Unknown Source)
      at org.apache.poi.xssf.usermodel.XSSFCell.&lt;init&gt;(XSSFCell.java:105)
      at org.apache.poi.xssf.usermodel.XSSFRow.&lt;init&gt;(XSSFRow.java:70)
      at org.apache.poi.xssf.usermodel.XSSFSheet.initRows(XSSFSheet.java:179)
      at org.apache.poi.xssf.usermodel.XSSFSheet.read(XSSFSheet.java:143)
      at org.apache.poi.xssf.usermodel.XSSFSheet.onDocumentRead(XSSFSheet.java:130)
      at org.apache.poi.xssf.usermodel.XSSFWorkbook.onDocumentRead(XSSFWorkbook.java:286)
      at org.apache.poi.POIXMLDocument.load(POIXMLDocument.java:159)
      at org.apache.poi.xssf.usermodel.XSSFWorkbook.&lt;init&gt;(XSSFWorkbook.java:207)
      at com.xls.validate.ExcelValidator.main(ExcelValidator.java:79)
  </code></pre>
  <p>This works perfectly fine when the .xlsx file is less than 1 MB.</p>
  <p>I understand this is because my .xlsx file is around 5-10 MB and POI tries to load the entire sheet at once in JVM memory.</p>
  <p>What can be a possible workaround?</p>