我们一直在使用 bone cp 进行池化。我们通过 mat 结果注意到它指向这个 com.mysql.jdbc.JDBC4Connection 并且内存使用量不断增加?下面是 2 个示例,我们可以看到内存使用量的增加。
样品 1
21 instances of "com.mysql.jdbc.JDBC4Connection", loaded by "sun.misc.Launcher$ExtClassLoader @ 0xf000a4f0" occupy 11,793,200 (76.19%) bytes.
Biggest instances:
•com.mysql.jdbc.JDBC4Connection @ 0xf016d728 - 958,096 (6.19%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf0094478 - 875,568 (5.66%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf0201ba0 - 816,048 (5.27%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf04ab3f0 - 754,016 (4.87%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf006ee40 - 727,024 (4.70%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf0076a90 - 663,872 (4.29%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf04ad490 - 618,200 (3.99%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf00b7bd0 - 616,608 (3.98%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf0467bc0 - 612,544 (3.96%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf015fcf0 - 598,400 (3.87%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf021c830 - 584,992 (3.78%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf0097818 - 561,224 (3.63%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf01a27c0 - 531,816 (3.44%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf00bea28 - 531,416 (3.43%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf01c2d80 - 522,184 (3.37%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf04693e0 - 482,992 (3.12%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf012b158 - 453,256 (2.93%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf147f438 - 424,656 (2.74%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf15ff7d0 - 187,008 (1.21%) bytes.
Keywords
com.mysql.jdbc.JDBC4Connection
sun.misc.Launcher$ExtClassLoader @ 0xf000a4f0
Reference Pattern
Class Name Shallow Heap Retained Heap Percentage
class java.lang.Thread @ 0xf0003840 40 208 0.00
样品 2
21 instances of "com.mysql.jdbc.JDBC4Connection", loaded by "sun.misc.Launcher$ExtClassLoader @ 0xf000a4f0" occupy 17,339,632 (80.79%) bytes.
Biggest instances:
•com.mysql.jdbc.JDBC4Connection @ 0xf016d728 - 1,228,048 (5.72%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf0094478 - 1,144,056 (5.33%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf0201ba0 - 1,126,120 (5.25%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf04ab3f0 - 1,074,552 (5.01%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf006ee40 - 993,912 (4.63%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf0076a90 - 931,512 (4.34%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf015fcf0 - 930,952 (4.34%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf04ad490 - 918,176 (4.28%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf00bea28 - 874,512 (4.07%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf0467bc0 - 846,368 (3.94%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf00b7bd0 - 838,448 (3.91%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf021c830 - 799,184 (3.72%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf0097818 - 791,256 (3.69%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf01a27c0 - 763,264 (3.56%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf01c2d80 - 745,088 (3.47%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf012b158 - 710,704 (3.31%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf04693e0 - 704,072 (3.28%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf147f438 - 680,400 (3.17%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf1603618 - 458,472 (2.14%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf148fd70 - 390,984 (1.82%) bytes.
•com.mysql.jdbc.JDBC4Connection @ 0xf15ff7d0 - 389,552 (1.82%) bytes.
Keywords
com.mysql.jdbc.JDBC4Connection
sun.misc.Launcher$ExtClassLoader @ 0xf000a4f0
Reference Pattern
Class Name Shallow Heap Retained Heap Percentage
class java.lang.Thread @ 0xf0003840 40 208 0.00%
样本浅层和保留堆
Class Name | Objects | Shallow Heap | Retained Heap
------------------------------------------------------------------------------------------------------------
| | |
byte[] | 19,197 | 4,502,128 | 4,502,128
char[] | 50,285 | 2,646,080 | 2,646,080
com.mysql.jdbc.JDBC4ResultSet | 9,427 | 1,659,152 | 12,354,360
java.util.HashMap$Entry[] | 19,246 | 1,641,928 | 16,387,720
java.lang.String | 51,067 | 1,634,144 | 3,951,064
com.mysql.jdbc.StatementImpl | 9,427 | 1,432,904 | 15,295,672
java.util.TreeMap | 28,285 | 1,357,680 | 2,495,568
com.mysql.jdbc.Field | 9,417 | 1,356,048 | 2,485,064
java.util.TreeMap$Entry | 28,420 | 1,136,800 | 1,137,856
java.util.HashMap | 19,224 | 922,752 | 16,401,088
java.util.HashMap$Entry | 22,253 | 712,096 | 16,255,280
java.lang.Object[] | 10,789 | 593,000 | 1,412,184
java.lang.Object | 20,583 | 329,328 | 329,328
com.mysql.jdbc.RowDataStatic | 9,417 | 301,344 | 1,054,704
java.net.SocksSocketImpl | 2,572 | 288,064 | 836,096
java.util.ArrayList | 9,729 | 233,496 | 830,136
com.mysql.jdbc.Field[] | 9,427 | 226,168 | 2,711,224
byte[][] | 9,417 | 226,008 | 452,648
com.mysql.jdbc.ByteArrayRow | 9,417 | 226,008 | 678,656
com.mysql.jdbc.ConnectionPropertiesImpl$BooleanConnectionProperty| 2,750 | 176,000 | 179,520
java.util.Hashtable$Entry | 4,837 | 154,784 | 295,040
java.util.HashSet | 9,460 | 151,360 | 1,372,632
Total: 22 of 2,427 entries; 2,405 more | 407,563 | 23,597,568 |
------------------------------------------------------------------------------------------------------------
对于插入和更新查询,我们这样做,下面我们确保关闭语句。
Statement stmt1 = null;
stmt1 = dbconn.createStatement();
String insertQuery3 =........
count = stmt9.executeUpdate(insertQuery3);
try{
if ( stmt1!= null ){ stmt1.close();
}
else{
System.out.println("No stm1 exist");
}
}catch(SQLException ex){ System.out.println("SQLException has been caught for stmt1");
ex.printStackTrace(System.out);
}
For select we do this
Statement stmt2 = null;
stmt2 = dbconn.createStatement();
String selectQuery2= .........
ResultSet rs2 = stmt2.executeQuery(selectQuery2);
if(rs2.next())
{
}
try{
if ( rs2!= null ){ rs2.close();
} else{
System.out.println("No rs2 exist");
}
if ( stmt2!= null ){ stmt2.close();
} else{
System.out.println("No stm2 exist");
}
}catch(SQLException ex){ System.out.println("SQLException has been caught for stmt2");
ex.printStackTrace(System.out);
}