thecaz
asked on
java.lang.OutOfMemoryError (creating Excel File with HSSF)
Hi all.
I'm trying to solve a outofmemory problem with a servlet creating a Excel file.
I've downloaded TOMCAT 5 and set
CATALINA_OPTS = -server -Xmx1200M -Xms1200M -Xss256k
Here there is the message "
"exception
javax.servlet.ServletExcep tion: Servlet execution threw an exception
root cause
java.lang.OutOfMemoryError "
At the end there is a sample servlet giving this error (Note that reducing the for cycle the error disappear! and this is normal!).
Anyone could solve this problem?
Thanks!!!!!!!!!!!!!!
Thecaz
Here there is teh Servlet
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.poi.hssf.usermo del.*;
public class PrimoServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void destroy() {
}
/** Processes requests for both HTTP GET and POST methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServlet Request request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("a pplication /vnd.ms-ex cel");
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
for (int i=0; i<10000; i++) {
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short)i);
// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(1);
// Or do it on one line.
row.createCell((short)1).s etCellValu e(1.2);
row.createCell((short)2).s etCellValu e("This is a string");
row.createCell((short)3).s etCellValu e(true);
row.createCell((short)4).s etCellValu e("This is a string1");
row.createCell((short)5).s etCellValu e("This is a string2");
row.createCell((short)6).s etCellValu e("This is a string1");
row.createCell((short)7).s etCellValu e("This is a string2");
row.createCell((short)8).s etCellValu e("This is a string1");
row.createCell((short)9).s etCellValu e("This is a string2");
row.createCell((short)10). setCellVal ue("This is a string1");
row.createCell((short)11). setCellVal ue("This is a string2");
row.createCell((short)12). setCellVal ue("This is a string1");
row.createCell((short)13). setCellVal ue("This is a string2");
row.createCell((short)14). setCellVal ue("This is a string1");
row.createCell((short)15). setCellVal ue("This is a string2");
row.createCell((short)16). setCellVal ue("This is a string1");
row.createCell((short)17). setCellVal ue("This is a string2");
row.createCell((short)20). setCellVal ue("This is a string1");
row.createCell((short)21). setCellVal ue("This is a string2");
row.createCell((short)22). setCellVal ue("This is a string1");
row.createCell((short)23). setCellVal ue("This is a string2");
row.createCell((short)24). setCellVal ue("This is a string1");
row.createCell((short)25). setCellVal ue("This is a string2");
row.createCell((short)26). setCellVal ue("This is a string1");
row.createCell((short)27). setCellVal ue("This is a string2");
}
// Write the output
OutputStream out = response.getOutputStream() ;
wb.write(out);
out.close();
}
/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/** Handles the HTTP POST method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Example to create a workbook in a servlet using HSSF";
}
}
I'm trying to solve a outofmemory problem with a servlet creating a Excel file.
I've downloaded TOMCAT 5 and set
CATALINA_OPTS = -server -Xmx1200M -Xms1200M -Xss256k
Here there is the message "
"exception
javax.servlet.ServletExcep
root cause
java.lang.OutOfMemoryError
At the end there is a sample servlet giving this error (Note that reducing the for cycle the error disappear! and this is normal!).
Anyone could solve this problem?
Thanks!!!!!!!!!!!!!!
Thecaz
Here there is teh Servlet
import java.io.*;
import java.net.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.poi.hssf.usermo
public class PrimoServlet extends HttpServlet {
public void init(ServletConfig config) throws ServletException {
super.init(config);
}
public void destroy() {
}
/** Processes requests for both HTTP GET and POST methods.
* @param request servlet request
* @param response servlet response
*/
protected void processRequest(HttpServlet
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("a
HSSFWorkbook wb = new HSSFWorkbook();
HSSFSheet sheet = wb.createSheet("new sheet");
for (int i=0; i<10000; i++) {
// Create a row and put some cells in it. Rows are 0 based.
HSSFRow row = sheet.createRow((short)i);
// Create a cell and put a value in it.
HSSFCell cell = row.createCell((short)0);
cell.setCellValue(1);
// Or do it on one line.
row.createCell((short)1).s
row.createCell((short)2).s
row.createCell((short)3).s
row.createCell((short)4).s
row.createCell((short)5).s
row.createCell((short)6).s
row.createCell((short)7).s
row.createCell((short)8).s
row.createCell((short)9).s
row.createCell((short)10).
row.createCell((short)11).
row.createCell((short)12).
row.createCell((short)13).
row.createCell((short)14).
row.createCell((short)15).
row.createCell((short)16).
row.createCell((short)17).
row.createCell((short)20).
row.createCell((short)21).
row.createCell((short)22).
row.createCell((short)23).
row.createCell((short)24).
row.createCell((short)25).
row.createCell((short)26).
row.createCell((short)27).
}
// Write the output
OutputStream out = response.getOutputStream()
wb.write(out);
out.close();
}
/** Handles the HTTP <code>GET</code> method.
* @param request servlet request
* @param response servlet response
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/** Handles the HTTP POST method.
* @param request servlet request
* @param response servlet response
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
processRequest(request, response);
}
/** Returns a short description of the servlet.
*/
public String getServletInfo() {
return "Example to create a workbook in a servlet using HSSF";
}
}
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I'm not sure how to do it with Windows, but this might work:
Right click on the icon that launches Tomcat. If you left click on properties and then click the shortcut tab you'll find an input box called target. You can specify options for java there. If that doesn't work I'm not sure how you would do it and hopefully someone else will be able to guide you.
-Pat
Right click on the icon that launches Tomcat. If you left click on properties and then click the shortcut tab you'll find an input box called target. You can specify options for java there. If that doesn't work I'm not sure how you would do it and hopefully someone else will be able to guide you.
-Pat
With POI, I think you need to build the whole thing in memory and then write it out. Thus, if you really do need 10,000 rows in the Excel table (!), then Tomcat will need the memory to store 10,000 rows. Following the trail pat5star started look promising.
ASKER
Hi all.
nggreer you are right. The first approach is impossible.
Now i'm trying mofifying the java opts and it seems working. I don't understand why JAVA_OPTS or CATALINA_OPTS doesn't work.
Bah!
TheCaz
nggreer you are right. The first approach is impossible.
Now i'm trying mofifying the java opts and it seems working. I don't understand why JAVA_OPTS or CATALINA_OPTS doesn't work.
Bah!
TheCaz
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
I know that 1200 is a strange value.
I forgot to say that I'm using tomcat under Windows XP and I lunch Tomcat using the shortcut (I heard that It could be a problem, I would like to lunch it manually but i'm not able)
I tried to modify catalina.bat putting on the head
CATALINA_OPTS="-Xms64m -Xmx384m"
It worked just 2 or 3 times and then nothing.
I trie to put the variable using Control-Panel -> System ecc ecc .. but nothing changed
Any idea?
Thecaz