【发布时间】:2012-01-11 16:46:50
【问题描述】:
我在 weblogic 上使用 spring jdbc。我将 fetch size 设置为 500,以便更快地从 db 中获取数据。但这会导致内存问题。这是一个例子:
http://webmoli.com/2009/02/01/jdbc-performance-tuning-with-optimal-fetch-size/
我的问题是如何释放这个内存?运行 GC 不工作,我猜它不工作,因为连接池中的连接是活动的。
代码:
public List<Msisdn> getNewMsisdnsForBulkSmsId(String bulkSmsId,String scheduleId,final int msisdnCount) throws SQLException {
JdbcTemplate jdbcTemplate = getJdbcTemplate();
jdbcTemplate.setFetchSize(500);
jdbcTemplate.setMaxRows(msisdnCount);
jdbcTemplate.query("select BULKSMS_ID, ? as , STATUSSELECTDATE, DELIVERYTIME, ID, MESSAGE from ada_msisdn partition (ID_"+bulkSmsId+") where bulksms_id = ? and status = 0 and ERRORCODE = 0 and SCHEDULEID is null for update skip locked", new Object[]{scheduleId,bulkSmsId}, MsisdnRowMapper.INSTANCE);
//Also i tried to close connection and run gc, this does not free the memory too.
//jdbcTemplate.getDataSource().getConnection().close();
//System.gc();
return null;
}
当我将 fetch size 设置为 10 时,堆大小为 12 MB 如果我将 fetch size 设置为 500,则堆大小为 206 MB
感谢
【问题讨论】:
-
如果您使用分析器,您应该能够看到为什么要保留此内存。
-
似乎连接正在分配内存,在我的代码中没有内存泄漏:60.9% - 206 MB - 561,359 分配。 org.springframework.jdbc.core.JdbcTemplate.query
-
如果您将 fetch size 设置为 10,那么问题真的会消失吗?
-
是的,但此时查询时间过长。获取 500 x 500 的数据要快得多。
标签: java sql memory jdbc fetch