background
Took over a previously resolved Excel projects using Java components in the POI resolved, but often OOM parsing time, then I optimize the following aspects from under 99% of problem solving, yes, you did not wrong, addressed only 99%.
solution
- Adjust the JVM heap memory
We know that almost all the java object instances are stored in the Java heap, there is certainly OOM heap memory is not enough, all to emphasize piles of memory.
The following command to start the JVM heap after the initial memory and maximum memory is adjusted to 4g:
java -Xms4g -Xmx4g
复制代码
- Excel size limit
Data take up more memory on the big, after a period of time is observed that some excel pictures or have dozens sheet page, but really need to parse the data probably hundreds of lines, so limit the time directly upload Excel files size.
- POI modify the source code
After the above two programs despite low frequency OOM, but still there, and later found a few packets of Excel tracking POI OOM source, we found a lot of empty rows POI objects are created, modified to direct the blank line does not handle like a.
POI version used in the project is 3.17, the modification is XSSFSheet.java initRows () method code, change the following, in fact, an increase of only three lines of code (curly braces):
private void initRows(CTWorksheet worksheetParam) {
_rows.clear();
tables = new TreeMap<String, XSSFTable>();
sharedFormulas = new HashMap<Integer, CTCellFormula>();
arrayFormulas = new ArrayList<CellRangeAddress>();
for (CTRow row : worksheetParam.getSheetData().getRowArray()) {
//修改poi源码 begin
if(row.getCArray().length<=0){
continue;
}
//修改poi源码 end
XSSFRow r = new XSSFRow(row, this);
// Performance optimization: explicit boxing is slightly faster than auto-unboxing, though may use more memory
final Integer rownumI = new Integer(r.getRowNum()); // NOSONAR
_rows.put(rownumI, r);
}
}
复制代码
If the above three programs is not enough, consider using easyexcel, alibaba open source, based on notes and readable, want to know more can refer to: github.com/alibaba/eas...
Recommended Reading
1. hands with you with a complete database middleware Mycat + SpringBoot sub-library sub-table
2. Inventory 35 Java code optimization details
4. take you one minute to understand the dynamic SQL under the MyBatis!
5. take you one minute at Spring Security!
If you think the article is good, hope can readily forwarded or "look" Oh, thank you very much ha!
No reply after public attention under the "1024" have a surprise for you!