how to overcome out of memory exception

Hi all,

My Application is the one which is accessed by so many people via intranet...

I have used connection Pooling concept to esatblish connection to Databse.

i have created a datsource via tomcat admin page and set it as a attribute in ServletContext through a listener class.

Then fetching that datasource getServletContext().getAttribute method.

Using that datasource fetched I am opening connection in starting of try block in every page and closing the connection in Finally block.

I am using Tomcat with MySql .

I am getting Out ofMemory error often, after which i need to restart the tomcat server to get rid of this error and make my application work normally......

Can any body help me and suggest me how to overcome this error...?

[800 byte] By [sreand_1428a] at [2007-11-27 1:57:03]
# 1
I sugest you do some profiling of your application to see what is causing this. Other thing is, maybe tomcat is set with default paramas for memory consumption (i don't know the defaults) but you could try pushing those values up if needed.
kimatrixa at 2007-7-12 1:32:09 > top of Java-index,Java Essentials,Java Programming...
# 2

> Can any body help me and suggest me how to overcome

> this error...?

Don't cache anything. Don't put objects in a user's session - in fact never use the session. If you need some minimal state then pass it back and forth between the client and server in an encrypted Cookie or using URL rewriting. If you use this approach then put a timeout within the Cookie each time it is sent to the client and check that timeout each time the Cookie is presented back at the server.

sabre150a at 2007-7-12 1:32:09 > top of Java-index,Java Essentials,Java Programming...
# 3

Hi,

I am not storing only userid of the persons logged in as a session attribute. nothing else.

But i am using Hashtables only throughout all jsp files for storing records of all resultset.

is that be a reason for causing out of memory exception.

Can u explain how to make the hash table references and their keys and values to null ......?

Help me please.... :--(

sreand_1428a at 2007-7-12 1:32:09 > top of Java-index,Java Essentials,Java Programming...
# 4

Hi,

Correction in previous reply in first sentence ..

I am storing only userid of the persons logged in as a session attribute. nothing else.

But i am using Hashtables only throughout all jsp files for storing records of all resultset.

is that be a reason for causing out of memory exception.

Can u explain how to make the hash table references and their keys and values to null ......?

Help me please.... :--(

sreand_1428a at 2007-7-12 1:32:09 > top of Java-index,Java Essentials,Java Programming...
# 5

> is that be a reason for causing out of memory

> exception.

>

Since we have no idea what you code looks like how can we know what is causing your problem. From your statement about ResultSet I suspect you are just trying to display too much data but without seeing your code and knowing the size of the data you are trying to display how can I really know?

sabre150a at 2007-7-12 1:32:09 > top of Java-index,Java Essentials,Java Programming...
# 6

<%@ page import="java.io.*,java.sql.*,javax.sql.*,javax.servlet.jsp.JspPage,java.net.*,java.util.*,com.pts.database.DBCon" %>

<%@ page import="com.pts.beans.ProjMeetFormat,com.pts.beans.BProjStatus" errorPage="../../InterError.jsp"%>

<jsp:useBean id="projs" class="com.pts.beans.BProjStatus" scope="page" />

<%!

DataSource datasource=null;

public void jspInit()

{

try

{

System.out.println("get attribute of DBCPool: "+getServletContext().getAttribute("DBCPool"));

datasource =(DataSource) getServletContext().getAttribute("DBCPool");

System.out.println("datasource inside ViewStatsReport.jsp new : " + datasource);

}

catch(Exception e)

{

System.out.println("exception in getting datasource in ViewStatsReport.jsp" + e.getMessage());

}

}

%>

<%

Connection con=null;

Statement stmt1=null;

ResultSet rs=null;

ResultSet rs1=null;

ResultSet rs2=null;

ResultSet rs3=null;

ResultSet rs4=null;

try

{

con=datasource.getConnection();

System.out.println("after static connection is new" + con);

stmt1=con.createStatement();

String sql1,sql,sql2,sql3,proc="",stat="",ad="",prosid="";

int count1=0,count2=0,total=0,i,chapnum=0,j;

String[] cids= new String[100];

String duedateDup="",duedate="",projType="",projName="",ap=null;boolean meet=false,heading=true;

// code for checking TE/TL

String uname=(String)session.getAttribute("uname");

System.out.println("inside select client is " + uname);

String check_TL="";

String trim_projid="";

String check_TE="";

int TL_indexof=0;

int TL_strlength=0;

boolean check_TL_flag=false;

String strPrID = request.getParameter("projID");

TL_indexof=strPrID.indexOf("P");

TL_strlength=strPrID.length();

trim_projid=strPrID.substring(TL_indexof,TL_strlength);

rs=stmt1.executeQuery("select u.userid from user_det u,proj_det p,proj_handler ph where p.projid='"+trim_projid+"' and u.username='"+uname+"' and u.userid=ph.userid and p.projid=ph.projid ");

if(rs.next())

{

check_TL_flag=true;

}

else{

rs=stmt1.executeQuery("select * from user_det where username='"+uname+"' and designame in('Team Manager','Production','Management')");

if(rs.next())

{

check_TL_flag=true;

}

}

System.out.println("check_TL_flag"+check_TL_flag);

if(!check_TL_flag)

{

RequestDispatcher rd = request.getRequestDispatcher("ViewStatusReportothers.jsp");

rd.forward(request,response);

}

// end of TE/TL code

Hashtable chapNames = new Hashtable();

Hashtable chapIDS = new Hashtable();

Hashtable prosNames = new Hashtable();

if(strPrID.startsWith("R6"))

{

RequestDispatcher rd = request.getRequestDispatcher("FinalReport.jsp");

rd.forward(request,response);

}

String cidqry="";

rs = stmt1.executeQuery("select chapID,chapName from chapstat where projID='"+strPrID+"' order by chapid");

for(i=0; rs.next(); i++)

{

cids=rs.getString("chapID");

chapIDS.put(""+i,""+rs.getString("chapID"));

chapNames.put(""+i,rs.getString("chapName"));

}

chapnum=i;

sql = "select projname,projType,duedateDup,duedate,activeprocess from proj_det where projID='"+strPrID+"'";

rs = stmt1.executeQuery(sql);

while(rs.next())

{

projName=rs.getString("projName");

projType = rs.getString("projType");

duedateDup = rs.getString("duedateDup");

duedate = rs.getString("duedate");

ap=rs.getString("activeprocess");

}

sql1="select distinct p.prosid,pd.process from projstat p,process_det pd where projID='"+strPrID+"' && status != 'NA' && p.prosid=pd.prosid && p.prosid not in ('PR19','PR20','PR21') order by pd.porder";

rs2=stmt1.executeQuery(sql1);

and etc.........

This is a piece of code in the page which is visited most often by the endusers. To be frank, in all the jsp pages, I am using Hashtables like this to store results of query . After storing in Hashtables, i am using the vvalues present in Hashtable for any calculations or display. First part of my code in any jsp will be like fetching from database and storing in Hashtables. In the remaining part of my code. I will be fetching stored values in hashtable and using it for display and calculation.

can u suggest any remedy now for my problem...?

sreand_1428a at 2007-7-12 1:32:09 > top of Java-index,Java Essentials,Java Programming...
# 7

> can u suggest any remedy now for my problem...?

1) You did not say how big your result sets are.

2) Move all the Java you can (especially the JDBC stuff) out of your JSP and put in a separate class. This will make it testable without the baggage of the JSP.

3) Profile the new class to see what memory it is using.

sabre150a at 2007-7-12 1:32:09 > top of Java-index,Java Essentials,Java Programming...
# 8
Hi, Thanks for your suggestion of using a seperate class jdbc stuffs.Size of my resulset will at the minimum of 10 , may be at the maximum of even beyond 500, even thousand at sometimes. What can be done now...? Help me to rectify this problem plz....
sreand_1428a at 2007-7-12 1:32:09 > top of Java-index,Java Essentials,Java Programming...