title: GC overhead limit exceeded with Apache POI
body: <p>I have 13 .xlsx files with about 1000 rows in each of them. Now I want to merge it to one .xlsx file with one sheet. I'm using code from here
  <a href="https://blog.sodhanalibrary.com/2014/11/merge-excel-files-using-java.html#.Vi9ns36rSUk" rel="nofollow noreferrer">https://blog.sodhanalibrary.com/2014/11/merge-excel-files-using-java.html#.Vi9ns36rSUk</a>.</p>
  <p>Here's my code (few changes, addSheet method unchanged)</p>
  <pre><code>try {
          FileInputStream excellFile1 = new FileInputStream(new File(&quot;tmp_testOut1000.xlsx&quot;));
          XSSFWorkbook workbook1 = new XSSFWorkbook(excellFile1);
          XSSFSheet sheet1 = workbook1.getSheetAt(0);
  
          for(int i = 2; i &lt; 14; i++){
              FileInputStream excellFile2 = new FileInputStream(new File(&quot;tmp_testOut&quot; + i + &quot;000.xlsx&quot;));
              XSSFWorkbook workbook2 = new XSSFWorkbook(excellFile2);
              XSSFSheet sheet2 = workbook2.getSheetAt(0);
              System.out.println(&quot;add &quot; + i);
              addSheet(sheet1, sheet2);
          }
          
          excellFile1.close();
  
          // save merged file
          System.out.println(&quot;merging&quot;);
          File mergedFile = new File(&quot;merged.xlsx&quot;);
          if (!mergedFile.exists()) {
              mergedFile.createNewFile();
          }
          FileOutputStream out = new FileOutputStream(mergedFile);
          System.out.println(&quot;write&quot;);
          workbook1.write(out);
          out.close();
          System.out.println(&quot;Files were merged succussfully&quot;);
      } catch (Exception e) {
          e.printStackTrace();
      }
  </code></pre>
  <p>All files are loading and merging but after &quot;write&quot; sysout I'm getting</p>
  <pre><code>Exception in thread &quot;main&quot; java.lang.OutOfMemoryError: GC overhead limit exceeded
  at org.apache.xmlbeans.impl.store.Xobj.new_cursor(Xobj.java:1829)
  at org.apache.xmlbeans.impl.values.XmlObjectBase.newCursor(XmlObjectBase.java:293)
  at org.apache.xmlbeans.impl.values.XmlComplexContentImpl.arraySetterHelper(XmlComplexContentImpl.java:1151)
  at org.openxmlformats.schemas.spreadsheetml.x2006.main.impl.CTFontsImpl.setFontArray(Unknown Source)
  at org.apache.poi.xssf.model.StylesTable.writeTo(StylesTable.java:424)
  at org.apache.poi.xssf.model.StylesTable.commit(StylesTable.java:496)
  at org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:341)
  at org.apache.poi.POIXMLDocumentPart.onSave(POIXMLDocumentPart.java:345)
  at org.apache.poi.POIXMLDocument.write(POIXMLDocument.java:206)
  at Start.main(Start.java:275)
  </code></pre>
  <p>What can I do? Why is this happening and how to prevent it?</p>