java.lang.OutOfMemoryError

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • nickyeng
    Contributor
    • Nov 2006
    • 252

    java.lang.OutOfMemoryError

    I keep getting this error:
    Code:
    java.lang.OutOfMemoryError: Java heap space
    what possible reason that cause this error ?

    My boss dont want increase java memory, so i had to change my code.

    My code have sql statement and my database table has 60000 rows(record) and keep increasing.

    I call this to retrieve data from table:
    Code:
    double total = Billing.getTotalAmount(String str);
    so in that Billing class has a method called getTotalAmount which doing the sql statement and sum the amount for certain column amount and return it.

    Still, it can't runs and give same error.

    Any idea ?

    thanks in advance
    from
    Nick
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by nickyeng
    I keep getting this error:
    Code:
    java.lang.OutOfMemoryError: Java heap space
    what possible reason that cause this error ?

    My boss dont want increase java memory, so i had to change my code.

    My code have sql statement and my database table has 60000 rows(record) and keep increasing.

    I call this to retrieve data from table:
    Code:
    double total = Billing.getTotalAmount(String str);
    so in that Billing class has a method called getTotalAmount which doing the sql statement and sum the amount for certain column amount and return it.

    Still, it can't runs and give same error.

    Any idea ?

    thanks in advance
    from
    Nick
    You may need to post the getTotalAmount method ...

    Comment

    • nickyeng
      Contributor
      • Nov 2006
      • 252

      #3
      I didn't do like this until my boss ask me to give better solution to that OutOfMemoryErro r....

      so this is the changed code after OutOfMemoryErro r, but still, it gets the same Error.

      this is the code :

      Code:
      double total = 0;
      Connection conn = DbConnectionManager.getConnection();
      		try{
      			PreparedStatement st = conn.prepareStatement("select sum(amount) from billing where no like \'"+no+"%\' and  billing_date like \""+String.valueOf(year)+"%\" ");
      			ResultSet rs = st.executeQuery();
      			while(rs.next())
      				total = rs.getDouble(1);
      			
      			rs.close();
      			st.close();
      
                              return total;
      		}finally
      		{
      			DbConnectionManager.release(conn);
      		}

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by nickyeng
        I didn't do like this until my boss ask me to give better solution to that OutOfMemoryErro r....

        so this is the changed code after OutOfMemoryErro r, but still, it gets the same Error.

        this is the code :

        Code:
        double total = 0;
        Connection conn = DbConnectionManager.getConnection();
        		try{
        			PreparedStatement st = conn.prepareStatement("select sum(amount) from billing where no like \'"+no+"%\' and  billing_date like \""+String.valueOf(year)+"%\" ");
        			ResultSet rs = st.executeQuery();
        			while(rs.next())
        				total = rs.getDouble(1);
        			
        			rs.close();
        			st.close();
        
                                return total;
        		}finally
        		{
        			DbConnectionManager.release(conn);
        		}
        And approximately how many records do you have in that table?
        You don't need a while there you can replace that with an if as in
        [CODE=java]if(rs.next()) {
        total = rs.getDouble(1) ;
        }
        [/CODE]

        Comment

        • nickyeng
          Contributor
          • Nov 2006
          • 252

          #5
          Originally posted by r035198x
          And approximately how many records do you have in that table?
          You don't need a while there you can replace that with an if as in
          [CODE=java]if(rs.next()) {
          total = rs.getDouble(1) ;
          }
          [/CODE]
          60000 records and keep increasing.

          Comment

          • r035198x
            MVP
            • Sep 2006
            • 13225

            #6
            Originally posted by nickyeng
            60000 records and keep increasing.
            You did put printlns before and after the sql is executed to make sure that it's the querying that taking up all the memory?
            Perhaps you should increase the heap size then (See the JVM thread in this forum).
            Isn't there a way of optimizing your query?

            Comment

            • nickyeng
              Contributor
              • Nov 2006
              • 252

              #7
              Originally posted by r035198x
              You did put printlns before and after the sql is executed to make sure that it's the querying that taking up all the memory?
              Perhaps you should increase the heap size then (See the JVM thread in this forum).
              Isn't there a way of optimizing your query?

              i was using the for loop in my JSP to loops Billing object that returns by a method which is simple query like :
              Code:
              select count(*) from table1 where name = 'nick' and year = 'year';
              that Billing object consist of all(60000+) records, and then loops it and total up the amount.

              This BOLD part was the original code i've done.

              So i package up project as war file, and run in my server, it gives me OutOfMemoryErro r: java heap space.

              So, boss ask me to change my code so that let it be more simpler and take lesser memory because he dont want increase the java memory(jvm thing).

              In the end, i came up with the code i posted in the 1st post.

              now, i still getting the same error.
              haiz.

              Comment

              Working...