memory leak
843841Sep 27 2004 — edited Sep 28 2004Hi all,
I am a student and new to Servlet. I need your help urgently, because of an assignment.
I think I have memory leak in the following code, because when I access this page every time a lot memory is consumed,
I can see this in task manager, so after some time Tomcat5 response goes very slow...
I am struggling, what is wrong with this code... I know that I am creating and closing
DB connection every time, but this should not use so much memory...
Could some body help me....I would be very very thank full of you all.
Khan
public class ProspectiveFacts extends HttpServlet
{ private Connection con = null;
private PrintWriter out = null;
private PreparedStatement pAllFact = null;
private ResultSet rsAllFact = null;
private ResultSet rsFactValueID = null;
private ResultSet rsPFacts = null;
private ResultSet rsFactCount = null;
private PreparedStatement pFactValueID = null;
private PreparedStatement pPFacts = null;
private PreparedStatement pGetNextFreeID = null;
private PreparedStatement pFactCount = null;
private final static String ALL_FACT = "SELECT FactName_ID,FactName, DataType FROM FACT_DATATYPE ,FACT_NAME Where FACT_DATATYPE.FactDType_ID = FACT_NAME.FactDType_ID order by FactName_id";
private final static String FACT_VALUE_ID = "SELECT FactValue_ID from Fact_Name_Value where FactName_id=?";
private final static String P_FACT = "SELECT FactValueSymbol, FactValue from FACT_VALUE where factValue_ID=?";
private final static String FACT_DATA_TYPE = "SELECT DataType FROM FACT_DATATYPE WHERE FactType_ID = ?";
public void doGet(HttpServletRequest req, HttpServletResponse res)
throws ServletException, IOException
{
res.setContentType("text/html");
out = res.getWriter();
HtmlHelper.createHeader("Existing Prospective Facts... ", out);
try
{
InitialContext initCtx = new InitialContext();
DataSource ds = (DataSource) initCtx
.lookup("java:comp/env/jdbc/dbKASTerGrid");
con = ds.getConnection();
pAllFact = con.prepareStatement(ALL_FACT);
pFactValueID = con.prepareStatement(FACT_VALUE_ID);
pPFacts = con.prepareStatement(P_FACT);
rsAllFact = pAllFact.executeQuery();
HtmlHelper.createTableHeader(out, "Existing Prospective Facts");
while (rsAllFact.next())
{
String factName = rsAllFact.getString(2);
int factNameID = rsAllFact.getInt(1);
out.println("<tr><td><a href=\"/servlet/EditProsFact?factName="
+ factName + "&factNameID=" + factNameID + "\" >");
out.println(factName);
out.println("</a></td>");
pFactValueID.setInt(1, factNameID);
rsFactValueID = pFactValueID.executeQuery();
out.println("<td>");
while (rsFactValueID.next())
{
pPFacts.setInt(1, rsFactValueID.getInt(1));
rsPFacts = pPFacts.executeQuery();
while (rsPFacts.next())
{
out.println(rsPFacts.getString(1));
out.println("<br>");
}
}
out.println("</td>");
rsFactValueID = pFactValueID.executeQuery();
out.println("<td>");
while (rsFactValueID.next())
{
pPFacts.setInt(1, rsFactValueID.getInt(1));
rsPFacts = pPFacts.executeQuery();
while (rsPFacts.next())
{
out.println(rsPFacts.getString(2));
out.println("<br>");
}
}
out.println("</td>");
out.println("<td>" + rsAllFact.getString(3) + "</td>");
out.println("</tr>");
}
HtmlHelper.createTableFooter(out, "createNewFact.html",
" Create New Prospective Fact");
destroy();
}
catch (Exception e)
{
createExceptionPage(e);
destroy();
}
}
public void destroy()
{
try
{
if (con != null)
{
con.close();
rsAllFact.close();
rsFactCount.close();
rsFactValueID.close();
rsPFacts.close();
}
pAllFact = null;
pFactCount = null;
pFactValueID = null;
pPFacts = null;
}
catch (Exception ignore)
{
}
}
private void createExceptionPage(Exception e)
{
out.println("<HTML>\n\t<HEAD>\n\t\t<TITLE>" + Error...
+ "\n\t\t</TITLE>\n\t</HEAD>");
out.println("<BODY>");
out.println("An error has occured, please try again");
out.println("Sorry for any inconvenence....");
out.println("<BR>");
e.printStackTrace(out);
out.println("<BR>");
out.println("</BODY>");
out.println("</HTML>");
}
}