By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
444,136 Members | 2,135 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 444,136 IT Pros & Developers. It's quick & easy.

java.lang.OutOfMemoryError

100+
P: 254
I keep getting this error:
Expand|Select|Wrap|Line Numbers
  1. java.lang.OutOfMemoryError: Java heap space
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1. double total = Billing.getTotalAmount(String str);
  2.  
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
Sep 18 '07 #1
Share this Question
Share on Google+
6 Replies


10K+
P: 13,264
I keep getting this error:
Expand|Select|Wrap|Line Numbers
  1. java.lang.OutOfMemoryError: Java heap space
  2.  
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:
Expand|Select|Wrap|Line Numbers
  1. double total = Billing.getTotalAmount(String str);
  2.  
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 ...
Sep 18 '07 #2

100+
P: 254
I didn't do like this until my boss ask me to give better solution to that OutOfMemoryError....

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

this is the code :

Expand|Select|Wrap|Line Numbers
  1. double total = 0;
  2. Connection conn = DbConnectionManager.getConnection();
  3.         try{
  4.             PreparedStatement st = conn.prepareStatement("select sum(amount) from billing where no like \'"+no+"%\' and  billing_date like \""+String.valueOf(year)+"%\" ");
  5.             ResultSet rs = st.executeQuery();
  6.             while(rs.next())
  7.                 total = rs.getDouble(1);
  8.  
  9.             rs.close();
  10.             st.close();
  11.  
  12.                         return total;
  13.         }finally
  14.         {
  15.             DbConnectionManager.release(conn);
  16.         }
  17.  
  18.  
  19.  
Sep 18 '07 #3

10K+
P: 13,264
I didn't do like this until my boss ask me to give better solution to that OutOfMemoryError....

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

this is the code :

Expand|Select|Wrap|Line Numbers
  1. double total = 0;
  2. Connection conn = DbConnectionManager.getConnection();
  3.         try{
  4.             PreparedStatement st = conn.prepareStatement("select sum(amount) from billing where no like \'"+no+"%\' and  billing_date like \""+String.valueOf(year)+"%\" ");
  5.             ResultSet rs = st.executeQuery();
  6.             while(rs.next())
  7.                 total = rs.getDouble(1);
  8.  
  9.             rs.close();
  10.             st.close();
  11.  
  12.                         return total;
  13.         }finally
  14.         {
  15.             DbConnectionManager.release(conn);
  16.         }
  17.  
  18.  
  19.  
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
Expand|Select|Wrap|Line Numbers
  1. if(rs.next()) {
  2.       total = rs.getDouble(1); 
  3. }
  4.  
Sep 18 '07 #4

100+
P: 254
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
Expand|Select|Wrap|Line Numbers
  1. if(rs.next()) {
  2.       total = rs.getDouble(1); 
  3. }
  4.  
60000 records and keep increasing.
Sep 18 '07 #5

10K+
P: 13,264
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?
Sep 18 '07 #6

100+
P: 254
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 :
Expand|Select|Wrap|Line Numbers
  1. select count(*) from table1 where name = 'nick' and year = 'year';
  2.  
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 OutOfMemoryError: 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.
Sep 18 '07 #7

Post your reply

Sign in to post your reply or Sign up for a free account.