在 本機上用eclipse導出一百多萬的數據導出,沒有問題,但是部署在應用服務器上就會內存溢出,而且我把本機的tomcat也拷貝過去了。其它一個功能,比這數據還多幾W,只不過少寫出了幾個字段,也能正常導出。
int totalSize = behaviorPortraitService.querySiletUserPortraitCountMonth(sietUserPortraitForm.getParams());
int pageNum = 0;
if (totalSize % 1000000 == 0) {
pageNum = totalSize / 1000000;
}else{
pageNum = totalSize / 1000000 + 1;
}
write.append(" 年/月 IMSI 手機號碼 沉默天數 開卡時間"
+ enter);
outSTr = response.getOutputStream(); // 建立
buff = new BufferedOutputStream(outSTr);
// 循環總頁數,得到每頁的頁數
for (int i = 1; i <= pageNum; i++) {
// 根據頁數,得到 PageResult對象
list = behaviorPortraitService.exportSiletUserPortraitMonth(sietUserPortraitForm.getParams(),i,100000);
// 循環List集合進行寫入
for (int j = 0; j < list.size(); j++) {
// 循環集合,寫入數據到TXT中
write.append(list.get(j).getBegin_endtime());
write.append(" " + list.get(j).getImsi());
write.append(" " + list.get(j).getMsisdn());
write.append(" "+ list.get(j).getSilencedays());
write.append(" " + list.get(j).getStarttime()
+ enter);
}
// 寫完一個分頁對象之後,清空List,清楚緩存
list.clear();
System.gc();
}
buff.write(write.toString().getBytes("UTF-8"));
錯誤信息:
org.springframework.web.util.NestedServletException: Handler processing failed; nested exception is java.lang.OutOfMemoryError: Java heap space
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:839)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
com.trajectory.manager.controller.filter.SessionPrivaligeFilter.doFilter(SessionPrivaligeFilter.java:79)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
root cause
java.lang.OutOfMemoryError: Java heap space
java.lang.StringCoding$StringEncoder.encode(StringCoding.java:232)
java.lang.StringCoding.encode(StringCoding.java:272)
java.lang.String.getBytes(String.java:946)
com.trajectory.behaviorportrait.controller.ExportSiletUserPortraitController.writeWeekTxtWeek(ExportSiletUserPortraitController.java:209)
com.trajectory.behaviorportrait.controller.ExportSiletUserPortraitController.handleRequestInternal(ExportSiletUserPortraitController.java:69)
org.springframework.web.servlet.mvc.AbstractController.handleRequest(AbstractController.java:153)
org.springframework.web.servlet.mvc.SimpleControllerHandlerAdapter.handle(SimpleControllerHandlerAdapter.java:48)
org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:790)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:719)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:644)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:549)
javax.servlet.http.HttpServlet.service(HttpServlet.java:617)
javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
com.trajectory.manager.controller.filter.SessionPrivaligeFilter.doFilter(SessionPrivaligeFilter.java:79)
org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:88)
org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:76)
你創建了一個非常大的字符串,然後又轉換成一個非常大的 byte[]。
我建議把數據逐步寫入輸出流,不用使用 write.append,只使用二進制的 buf.write,如果你想寫文本,使用下面的代碼:
PrintWriter pw = new PrintWriter(new OutputStreamWriter(
new BufferedOutputStream(socket.getOuptutStream()), "UTF-8"));
pw.print( some text here );
pw.print( some more ext here );
pw.println( the end of the line );
pw.close(); //
不需要使用很多內存。