Results 1 to 16 of 16
  1. #1
    jeeva86ab is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default Out of Memory in heap-need solutions

    Dear friends,

    i am developing a real time web site, where i have to give results for students.
    Student hit ratio will be very high.I am using simple MVC pattern
    where html file request to a servlet and that servlet create a smiple java object,
    that java object will connect to the database and retrieve the data and give to that same servlet, this servlet Requestdispatch to a jsp page.

    Above thing is my scenario.

    As i described, the simple java class(which connect to database) is not delete the object which is used by it.I think this lead to Out of memory in heap.

    How i can delete the object once i used it ?
    If i even do it, whether GC will collect the unused object.

    Otherwise, may i write connection code in servlet itself(which get request from the html file) ?

    please give guidelines.
    reply will be highly appreciated.
    thax in adv.

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    How does the servlet create the object that does the connection.
    How does that object work?
    Without any code we really can't give a solution...as described you shouldn't be getting an OOM. That is, assuming the object created by the servlet isn't getting stored anywhere.

  3. #3
    jeeva86ab is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Post My source code

    My html page code(but it is in jsp):
    <head>
    <title></title>
    <!--<meta -equiv="Content-Type" content="text/html; charset=windows-1251">-->
    <meta -equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <link href="css/indexstyle.css" rel="stylesheet" type="text/css">
    <script language="javascript" >
    function focus(){

    var txtBox=document.getElementById("reg_txt");
    if (txtBox!=null ) txtBox.focus();

    }
    function printpage() {
    window.print();
    }

    function check(){
    if ((document.res.reg_txt.value.length <=8 || document.res.reg_txt.value.length >=12) || (document.res.reg_txt.value == null)){
    alert("Please fill the valid Registration number.");
    document.res.reg_txt.value="";
    focus();
    return false;
    }
    }


    </script>

    <style type="text/css">
    <!--
    body {
    background-color: #990000;
    }
    body,td,th {
    font-size: 14px;
    font-family: Arial, Helvetica, sans-serif;
    color: #ffffff;
    }
    .style1 {
    color: #f98A0F;
    font-weight: bold;
    }
    -->
    </style></head>
    <body height="0" onLoad="focus();">
    <center>


    <table width="961" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
    <td width="961">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr><td><hr width="1011" /></hr></td></tr>
    <tr>
    <td width="68%" bgcolor="990000" height="153" align='center'><div align="center"><a hrf="index.htmmll"><img srrrc="image" alt=" " width="854" height="126" border="0" longdesc="" /></a></div></td>
    </tr>
    <tr><td><hr width="1011" /></hr></td></tr>
    </table> </td>
    </tr>
    <tr>
    <td>
    <table width="99%" border="0" align="center" cellpadding="0" cellspacing="10">
    <tr><td ><div align="center">
    <p class="style1">Directorate of Distance Education Results </p>
    </div></td></tr>
    <tr>
    <td width="48%">
    <p>&nbsp;</p>
    <form action="dde.jj" method="post" name="res" onsubmit="return check()" id="res">
    <table width="404" border="0" align="center" cellpadding="1" cellspacing="1">
    <tr>
    <td><div align="center">Registration Number </div></td>
    <td><div align="center">
    <input name="reg" id="reg_txt" type="text" value="" size="20" maxlength="30" />
    </div></td>
    </tr>
    <tr>
    <td><input name="uri" type="hidden" value="root" />
    <input name="select" type="hidden" value="result" />&nbsp;</td>
    <td><div align="center">
    <input name="submit" type="submit" value="Submit" />
    </div></td>
    </tr>
    </table>
    </form>
    <p>&nbsp;</p></td>
    </tr>
    </table> </td>
    </tr>
    <tr><td><div align="right">
    <input name="print" type="button" value="PRINT RESULT" onclick="printpage();" disabled="disabled" />
    </div></td></tr>
    <tr><td><hr width="1011" /></hr></td></tr>
    </table>

    </center>
    </body>
    </html>

  4. #4
    jeeva86ab is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    My servlet code :(Controller)
    package com.alagappa.web;

    import javax.servlet.*;
    import javax.servlet.http.*;
    import java.io.*;
    import java.sql.*;
    import com.alagappa.model.*;
    import java.util.*;

    public class ResultServlet extends HttpServlet {

    ArrayList rs = null;
    String dbvalue ="";
    String regno="";
    String dburi="";
    public void init(ServletConfig config)
    throws ServletException {

    // Always pass the ServletConfig object to the super class
    super.init(config);
    }

    //Process the HTTP Get request
    public void doPost(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException {
    try
    {
    System.out.println("In servlet");
    SelectDataApp dataApp = new SelectDataApp();
    regno = request.getParameter("reg");
    dbvalue = request.getParameter("select");
    dburi = request.getParameter("uri");
    System.out.println("In servlet"+regno+dbvalue+dburi);
    rs = dataApp.selectData(dburi,dbvalue,regno);
    String error = dataApp.getError();
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();

    if(rs != null || (error.equals("")) )
    {
    System.out.println("In servlet7");
    request.setAttribute("rset",rs);
    System.out.println("In servlet8");
    RequestDispatcher view = request.getRequestDispatcher("dderesult.jsp");
    System.out.println("In servlet8.1");
    view.forward(request, response);
    System.out.println("In servlet9");
    }
    else{
    System.out.println("In servlet7 else"+rs);
    request.setAttribute("error",""+error);
    System.out.println("In servlet8 else");
    RequestDispatcher view = request.getRequestDispatcher("error.jsp");
    System.out.println("In servlet8.1 else");
    view.forward(request, response);
    System.out.println("In servlet9 else");

    }
    System.out.println("In servlet Nullifying Objects Starts");
    dataApp=null;
    regno=null;
    dburi=null;
    dbvalue=null;
    rs=null;
    System.out.println("In servlet Nullifying Objects Ends");

    }
    catch (Exception e)
    {
    System.err.println("" + e.getMessage());
    }

    }

    public void doGet(HttpServletRequest request,
    HttpServletResponse response)
    throws ServletException, IOException
    {
    try
    {
    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    out.println("Sorry ! Request Failed");
    out.close();
    }
    catch (Exception e)
    {
    System.err.println("" + e.getMessage());
    }
    }
    }

  5. #5
    jeeva86ab is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    My java code :(Model)
    package com.alagappa.model;

    import java.sql.*;
    import java.io.*;
    import java.util.*;

    public class SelectDataApp {

    ResultSet rs = null,rs1=null;
    ArrayList al = null;
    String Error = "";

    public SelectDataApp() {
    System.out.println("Constructor Created :"+Error);
    }
    public String getError(){
    return Error;
    }
    public void setError(String error){
    Error=error;
    }

    public ArrayList selectData(String dburi,String dbname,String regno)
    {

    Connection con = null;
    String coursecode = "";
    System.out.println("RegLen :" +regno.length());
    int startindex = regno.length()-7;
    coursecode = regno.substring(startindex,startindex+3);
    System.out.println("Course Code :" + coursecode);
    String tab_name="";

    try {

    // Load the Driver class file
    Class.forName("com.mysql.jdbc.Driver");

    // Make a connection to the ODBC datasource Movie Catalog
    con = DriverManager.getConnection("jdbc:mysql://localhost/"+dbname+"?user="+dburi+"&password=DatabaseAdmin~6 30003");
    System.out.println("Database connection established successfully..");
    if (con != null)
    {
    System.out.println("Database connection established inside..");
    // Create the Prepared statement
    PreparedStatement student_statement = null, course_statement = null;

    course_statement = con.prepareStatement("SELECT tablename FROM coursetable where courseid=?;");
    course_statement.setString(1, coursecode);
    rs1 = course_statement.executeQuery();
    if (rs1.next()) {
    // while (rs1.next())
    // {
    tab_name = rs1.getString(1);
    System.out.println("Table Name : " + tab_name);
    }
    else
    {
    Error = "Result Not Found";
    System.out.println("Prepared Statement 1 :"+Error);

    }
    // Student Statement
    if (Error.equals(""))
    {
    System.out.println("Prepared Statement 2 :"+Error);
    student_statement = con.prepareStatement("select * from " + tab_name + " where REGNO=?;");
    student_statement.setString(1, regno);
    rs = student_statement.executeQuery();
    al = resultSetToArrayList(rs);
    // System.out.println("Result set as Array"+al);
    }

    // Close the ResultSet
    rs1.close();
    rs.close();
    }
    else
    {
    Error = "Connection Failure";
    System.out.println("Conn :"+Error);
    // al = setError();
    }

    }

    catch (SQLException sqle) {
    Error ="Connection Failed";
    System.out.println("SQl :"+Error);
    System.err.println(sqle.getMessage());
    }
    catch (ClassNotFoundException cnfe) {
    Error ="Connection Not Found";
    System.out.println("CNF :"+Error);
    System.err.println(cnfe.getMessage());
    }
    catch (Exception e) {
    Error ="Result Not Found";
    System.out.println("Exception :"+Error);
    System.err.println(e.getMessage());
    }
    finally {

    try {

    if ( con != null ) {

    // Close the connection no matter what
    con.close();
    }
    }
    catch (SQLException sqle) {
    Error ="Connection Failed On closing";
    System.out.println("SQL Outside :"+Error+sqle.getMessage()+sqle.getStackTrace() );
    System.err.println(sqle.getMessage());
    }


    }

    System.err.println("Nullifying Objects Starts");
    dburi=null;
    dbname=null;
    regno=null;
    System.err.println("Nullifying Objects Ends");
    System.err.println("RS return here.. ");
    return al;
    }
    public ArrayList resultSetToArrayList(ResultSet rs)
    {
    System.err.println("ResultSetToArray calling.. ");
    ArrayList results = new ArrayList();
    ResultSetMetaData md=null;
    HashMap row=null;
    int columns=0;
    try
    {
    md = rs.getMetaData();
    columns = md.getColumnCount();
    row = new HashMap();
    if(rs.next())
    {
    results.add(row);
    for (int i = 1; i <= columns; i++)
    {
    // row.put(md.getColumnName(i), rs.getObject(i));
    if(rs.getString(i)!= null || rs.getString(i) != "")
    row.put(""+i, rs.getObject(i));
    }

    }
    else{
    Error ="Result Not Found";
    System.out.println("ResultSet Null :"+Error);
    return null;
    }
    }
    catch (Exception e)
    {
    Error ="Result Not Found";
    System.out.println("Exception ResultsetMETAdata :"+Error);
    System.err.println("Error :" + e.getMessage());
    }
    System.err.println("Nullifying Objects in ArrayMethods Starts");
    rs=null;
    row=null;
    md=null;
    System.err.println("Nullifying Objects in ArrayMethods Ends");
    return results;
    }

    /* public ArrayList setError() {
    ArrayList results = new ArrayList();
    HashMap row = new HashMap();
    results.add(row);
    row.put("C", Error);
    return results;
    } */

    }

  6. #6
    jeeva86ab is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    My JSP Result page code:(veiw)

    <%@ page language="java" import="java.sql.*,java.util.*"%>
    <html>
    <head><title> Exam Results</title>
    <script language="javascript" >
    function focus(){
    var txtBox=document.getElementById("reg_txt");
    if (txtBox!=null ) txtBox.focus();
    }
    function printpage() {
    window.print();
    }
    function check(){
    if ((document.res.reg_txt.value.length <=8 || document.res.reg_txt.value.length >=12) || (document.res.reg_txt.value == null)){
    alert("Please fill the valid Registration number.");
    document.res.reg_txt.value="";
    focus();
    return false;
    }
    }
    </script>
    <style type="text/css">
    <!--
    .style1 {font-size: 18px;
    font-weight: bold;
    }
    body,td,th {
    color: f98a0f;
    font-size: 14px;
    font-family: Arial, Helvetica, sans-serif;
    }
    body {
    background-color: #990000;
    }
    a:link {
    color: #fFFFFf;
    }
    a:visited {
    color: #f98a0f;
    }
    a:active {
    color: #f98a0f;
    }
    -->
    </style>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1"></head>
    <body>
    <table width="961" border="0" align="center" cellpadding="0" cellspacing="0">
    <tr>
    <td width="961">
    <table width="100%" border="0" cellspacing="0" cellpadding="0">
    <tr>
    <td><hr width="1011" /></hr></td>
    </tr>
    <tr>
    <td width="68%" bgcolor="990000" height="153" align='center'><div align="center"><img src="images/alagappa_logo.png" alt=" Result" width="854" height="126"></div></td>
    </tr>
    <tr>
    <td><hr width="1011" /></hr></td>
    </tr>
    </table>
    </td>
    </tr>
    <tr>
    <td>
    <table width="99%" border="0" align="center" cellpadding="0" cellspacing="10">
    <tr>
    <td ><div align="center">
    <p>DDE RESULTS - Nov-2009 </p>
    </div></td>
    </tr>
    <tr>
    <td>
    <table width="404" border="0" align="center" cellpadding="1" cellspacing="1">
    <tr>
    <td>
    <div align="center"> <CENTER>
    <p>
    <%
    ArrayList rows = new ArrayList ();
    if (request.getAttribute("rset") != null) {
    rows = (ArrayList) request.getAttribute("rset");
    HashMap p = (HashMap)rows.get(0);
    // if(((String)p.get("C")).equals("NO")) {
    %>
    </p>
    <table width="700" border="0" align="center" cellpadding="0" cellspacing="10">
    <tr>
    <td width="129">Name </td>
    <td width="541">: <% out.println(p.get(""+3)); %></td>
    </tr>
    <tr>
    <td>Registration No </td>
    <td>: <% out.println(p.get(""+2)); %></td>
    </tr>
    <tr>
    <td>Centre No </td>
    <td>: <% out.println(p.get(""+1)); %></td>
    </tr>
    </table>
    <TABLE width="700" BORDER="1" CELLPADDING="4" CELLSPACING="1">
    <TR>
    <TH>Subject Code</TH>
    <TH>Marks Obtained</TH>
    <TH>Result</TH>
    </TR>
    <tr>
    <%
    for (int j = 4; j < p.size()-1; j++) {
    %>
    <% if( ( (String) p.get(""+j)).length() >=1) {
    %>
    <tr>
    <td><center><% out.println(p.get(""+j++)); %></center></td><% } if( ( (String) p.get(""+j)).length() >=1) { %>
    <td><center><% out.println(p.get(""+j++)); %></center></td><% } if( ( (String) p.get(""+j)).length() >=1) { %>
    <td><center><% out.println(p.get(""+j)); %></center></td> <%
    }
    } %>
    </tr>
    </TABLE>
    <p>
    <%
    // }
    // else {
    // out.print((String)p.get("C"));
    // }
    }
    else
    {
    %>
    Connection Failure
    <%
    }
    %>
    </p>
    </CENTER>
    </div></td>
    </tr>
    </table>
    <p align="right"><a href="result.jsp">Click here to view another Result</a> </p></td>
    </tr>
    </table>
    </td>
    </tr>
    <tr>
    <td><div align="right">
    <input name="print" type="button" value="PRINT RESULT" onClick="printpage();" />
    </div></td>
    </tr>
    <tr>
    <td><hr width="1011" /></hr></td>
    </tr>
    </table>
    </body>
    </html>

  7. #7
    jeeva86ab is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    Please any suggestion :

    One more question:

    I have design my web page in html and saved it as JSP. Is there any change of have over memory usage by jsp (because no object is closed or nullified)?
    The pages may around 120 pages like this scenario.

    thax in adv

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    It's unformatted, so almost impossible to read.

    Saving your HTML as a JSP shouldn't cause a problem, though I would question why, unless you are planning on adding tags to it later on. If not, then set them up as HTML. It'll reduce the number of classes loaded if nothing else.

    But, as a pointer to a few things (though unlikely to be your OOM source)...

    1. never have attributes in a Servlet. It's not threadsafe at all.
    2. JSP shouldn't have code in it. This tends to imply business logic, and they're not for that...they're for display.
    3. Close all resources in a finally block...not just the connection, but the resultset and statement objects (resultset, then statement, then connection, in that order).
    4. Don't do this..."rs = null". It's a pointless waste of time in almost all cases.

  9. #9
    jeeva86ab is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Post thank for suggestion

    Dear friend,

    As you have given valuable suggestions, i have some doubt on it.

    In first one, you said don't use attribute.Can you suggest me some other way of doing this.

    In second one, in my JSP i use hash map to get data for presentation.if this is a bad programming, give me any other possible methods.

    Please give more suggestions.

    thax for you help.
    help for doing again.

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    Servlets are not one instance per session. They are shared, so using attributes is generally a no-no since one request could overwrite the attributes of another request causing all sorts of problems. That's what I mean by threading issues.

    As for the JSP, use tags. There's plenty of tag libraries out there. Generally you will pass either an object of a class to the page, or an array of objects which will usually be shown in a page. You shouldn't have to be extracting stuff from lists and maps.

    But that's a minor point compared to the first one. Sort that bit out first...

    And format your code and stick it in code tags when you post. If you post it again, don't bother with the HTML/JSP. It's unikely your problem is there.

  11. #11
    jeeva86ab is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Post I don't other way Kindly help me

    I know only this way, can you please give some other way of code.

  12. #12
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    Remove the attributes and declare them locally to the method they're used in.

    Even better, treat the servlet simply as a pass through to another object (layer) that handles all the "business" logic. That's, that does all the work of getting the data being requested. All the servlet does then is receives a request, calls the business layer, then controls where to send the response based on the result of the business layer call.

    I can't really explain it any better than that here. If you understand Java and servlets that should be more than sufficient for you to rework this.

  13. #13
    jeeva86ab is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Post thax

    thz for you adivce.
    please give me suggestion for java program(which is connected to database)

  14. #14
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    I have.
    That you're seemingly not taking it in implies to me that you really ought to run through the tutorials at Sun on all this.

  15. #15
    morya123 is offline Member
    Join Date
    Nov 2009
    Posts
    9
    Rep Power
    0

    Default

    use tomcat!!!

  16. #16
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,863
    Rep Power
    19

    Default

    A quick scan of the thread, so I might have missed it, but where does it say what server they're using?

    Or, to put it another way, that post was about as useful as posting:
    Drink beer!
    Smoke tabs!

Similar Threads

  1. JVm Heap memory settings
    By nagesh in forum Advanced Java
    Replies: 2
    Last Post: 09-17-2009, 05:47 PM
  2. Heap memory
    By gayathri_g in forum New To Java
    Replies: 12
    Last Post: 09-10-2009, 08:45 AM
  3. [SOLVED] JVM: Heap out of memory
    By jwilley44 in forum Eclipse
    Replies: 7
    Last Post: 02-01-2009, 04:13 PM
  4. Replies: 2
    Last Post: 08-21-2008, 07:33 PM
  5. JVM Heap memory settings
    By nagesh in forum New To Java
    Replies: 1
    Last Post: 08-11-2007, 10:17 PM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •