![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
Hi All. This test case tries to perform following steps: 1. connect to database 2. create table with 9 columns - col1 int, col2 varchar(255), col3 text, col4 int, col5 varchar(255), col6 text, col7 int, col8 varchar(255), col9 text 3. populate it with 200 rows - same values ('1','2','3','4','5','6','7','8','9') 4. try to retrieve 'col1' from every row 300*100 times 5. disconnect from database Unfortunately it throws java.lang.OutOfMemoryError during step 4. As you can see, we don't store retrieved data, so we don't consume any memory. We run this test under Borland OptimizeIt Profiler and ito shows that 99,9% memory consumes mysql connector. MySQL server: mysql Ver 14.7 Distrib 4.1.10, for pc-linux-gnu (i686) jdbc: mysql-connector-java-3.1.12-bin.jar Thanks for any help, Przemek [snip] |
|
for (int j = 0; j <= 100; j++) { sql = "SELECT * FROM " + TABLE_NAME + ";"; stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { rs.getString("col1"); } } } |
#3
| |||
| |||
|
|
Przemysław Klein wrote: Hi All. This test case tries to perform following steps: 1. connect to database 2. create table with 9 columns - col1 int, col2 varchar(255), col3 text, col4 int, col5 varchar(255), col6 text, col7 int, col8 varchar(255), col9 text 3. populate it with 200 rows - same values ('1','2','3','4','5','6','7','8','9') 4. try to retrieve 'col1' from every row 300*100 times 5. disconnect from database Unfortunately it throws java.lang.OutOfMemoryError during step 4. As you can see, we don't store retrieved data, so we don't consume any memory. We run this test under Borland OptimizeIt Profiler and ito shows that 99,9% memory consumes mysql connector. MySQL server: mysql Ver 14.7 Distrib 4.1.10, for pc-linux-gnu (i686) jdbc: mysql-connector-java-3.1.12-bin.jar Thanks for any help, Przemek [snip] for (int j = 0; j <= 100; j++) { sql = "SELECT * FROM " + TABLE_NAME + ";"; stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sql); while (rs.next()) { rs.getString("col1"); } } } Przemysław, You're creating result sets and statements here that don't ever get closed, and won't be GC'd until you close the connection (or run out of memory, in your case). There's no reason to create thousands of statements, one will be fine, create it before you enter the loop. Close the result sets in a finally block. (basically your code has created a memory leak because it's not respecting the lifecycle of JDBC objects). Your code should look something like this: try { stmt = conn.createStatement(); for (int j = 0; j <= 100; j++) { sql = "SELECT * FROM " + TABLE_NAME + ";"; ResultSet rs = null; try { rs = stmt.executeQuery(sql); while (rs.next()) { rs.getString("col1"); } } finally { if (rs != null) { rs.close(); } } } } } finally { if (stmt != null) stmt.close(); } -Mark |
![]() |
| Thread Tools | |
| Display Modes | |
| |