Original | how I solve the problem OOM POI parsing Excel emerging?

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

  1. 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
复制代码
  1. 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.

  1. 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

3. Ali Interviewer: micro-channel, respectively, to talk about the realization of the principle and log on Taobao scan code behind?

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!

Guess you like

Origin juejin.im/post/5e707316f265da57082a04a9