数据库有六十万数据,要进行批量更新
执行程序报错
java.lang.OutOfMemoryError: Java heap space
idea内存已改
-Xms1024m
-Xmx4096m
-XX:ReservedCodeCacheSize=1024m
设置了heap最大内存为4096之后
使用visualVM查看内存发现只用了2048就溢出了,,为啥没到4096呢。
代码如下
@Testpublic void testDBUpdateBatch() throws SQLException {
//三十万数据
//OK,查询数据用时:235 OK,查询数据用时:348 OK,查询数据用时:214
//OK,用时:6389 OK,用时:5179 OK,用时:5737
//OK,查询数据用时:165
//OK,用时:47915 大量时间消耗在for语句里面随机赋值
Connection conn = DBUtil.getConnection();
PreparedStatement psmt = null;
Long startTime1 = System.currentTimeMillis();
Statement stmt = DBUtil.getConnection().createStatement();
ResultSet rs = stmt.executeQuery("select id from Table1 where id > 7");
List list = new ArrayList<>();
while(rs.next()){//如果对象中有数据,就会循环打印出来
// System.out.println(rs.getInt("id")+","+rs.getString("column2"));
list.add(rs.getInt("id"));
}
Long endTime1 = System.currentTimeMillis();
System.out.println("OK,查询数据用时:" + (endTime1 - startTime1));
Long startTime = System.currentTimeMillis();
Random random =new Random();
float min = 1f;
float max = 100000000f;
try {
conn.setAutoCommit(false);
String sql = "update Table1 " +
"set idn = ? ,column2 = ? ,column2n = ? ,column3 = ?,column3n = ?,column4 = ?,column4n=?,column5=?,column5n=?,
column6=?,column6n=?,column7=?,column7n=?,column8=?,column8n=?,column9=?,column9n=?,column10=?,column10n=?,
column11=?,column11n=? " +
"where id = ?";
psmt= conn.prepareStatement(sql);