Reputation: 253
I am trying to generate Excel report by fetching data from DB and passing the pojo collection list to JRBeanCollectionDataSource. Trying to generate excel with 5000 records with 8 fields getting outofmemory issue.
I tried JRFileVirtualizer But no luck, can any one help me to get rid of this OutOfMemory issue. Below is my code
public class JasperReport {
public static void main(String [] args) throws Exception{
DBtoExcelDownloadDAO d = new DBtoExcelDownloadDAO();
List<ExcelDownloadPojo> results=d.getDBrecordsForDownload(168);
net.sf.jasperreports.engine.JasperReport jasperReport = JasperCompileManager
.compileReport("C:\\Users\\vkode200\\Networksolve_Report.jrxml");
String outputFileName = "C:\\Users\\vkode200\\Downloads\\Output\\person.xls";
JRFileVirtualizer virtualizer = new JRFileVirtualizer(1, "C:\\Users\\vkode200\\Reports");
JasperPrint jasperPrint = JasperFillManager.fillReport(jasperReport, (Map) getParameters(virtualizer),
new JRBeanCollectionDataSource(
findReportData(results),false));
JRXlsExporter exporterXls = new JRXlsExporter();
exporterXls.setParameter(JRXlsExporterParameter.JASPER_PRINT, jasperPrint);
exporterXls.setParameter(JRXlsExporterParameter.OUTPUT_FILE_NAME, outputFileName);
exporterXls.setParameter(JRXlsExporterParameter.IS_AUTO_DETECT_CELL_TYPE, Boolean.TRUE);
exporterXls.exportReport();
}
private static Collection findReportData(List<ExcelDownloadPojo> results) throws FileNotFoundException, IOException {
List<JasperPojo> list = SampleExcel.generateExcel(results);
return list;
}
private static Object getParameters(JRFileVirtualizer virtualizer ) {
Map<String, Object> params = new HashMap<String, Object>();
params.put("footerText", "Just to demonstrate how to pass parameters to report");
params.put("Title", "NETWORK_SOLVE");
params.put(JRParameter.REPORT_VIRTUALIZER,virtualizer);
return params;
}
}
Error:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space at org.apache.poi.hssf.usermodel.HSSFWorkbook.getBytes(HSSFWorkbook.java:1393) at org.apache.poi.hssf.usermodel.HSSFWorkbook.write(HSSFWorkbook.java:1292)
Upvotes: 0
Views: 2646
Reputation: 843
If you can't reduce the amount of data then you should increase the allocated heap for your application to prevent the exception. Since I don't know how your application works, here are some ways to solve this problem:
It is possible to increase heap size allocated to the JVM by using command line options
-Xms<size> set initial Java heap size
-Xmx<size> set maximum Java heap size
-Xss<size> set java thread stack size
java -Xms256m -Xmx2048m ClassName
Right click on project -> Properties -> Run
setenv.sh
or setenv.bat
is executed on startup. Create the file if it doesn't exist in $CATALINA_HOME/bin/
For Linux create setenv.sh
and add
export JAVA_OPTS="-server -Xmx2048m"
For Windows create setenv.bat
and add
set JAVA_OPTS=-server -Xmx2048m
Change the Xmx value to your needs.
Upvotes: 1