oracle 批量插入数据时, 运行一段时间就会报内存溢出
dump 内存之后发现T4CPreparedStatement 占用了83%的内存,并且老年代gc 没有生效,所以确定是T4CPreparedStatement对象导致内存溢出了
4,464 instances of "oracle.jdbc.driver.T4CPreparedStatement", loaded by "sun.misc.Launcher$AppClassLoader @ 0x6c0024168" occupy 3,215,859,904 (83.63%) bytes.
Keywords
oracle.jdbc.driver.T4CPreparedStatement
sun.misc.Launcher$AppClassLoader @ 0x6c0024168
查看mybatis对应的oracle批量插入语句为
<insert id="batchInsert" useGeneratedKeys="false">
insert into serial_no_14 (id,serial_no)
SELECT S_SERIAL_NO_14.NEXTVAL , A.* FROM (
<foreach collection="disruptSerialNoList" item="item" index="index" separator="UNION ALL">
SELECT #{item} FROM DUAL
</foreach>
) A
</insert>
可以看到批量插入语句每次执行,disruptSerialNoList列表大小不同,sql 都会不同; 所以我们这里看到有4,464个实例对象
由于配置的数据源默认max-open-prepared-statements是没有限制prepared-statement数量的,所以disruptSerialNoList列表大小不同时,都会新增一个prepared-statement对象。
找到问题了,修改max-open-prepared-statements参数就可以了,参数改为
spring.datasource.dbcp2.max-open-prepared-statements=20
再运行程序,使用jvisualvm 查看内存回收情况,就可以看到完美的锯齿线了;
老年代也不会被占满了,因为年轻代直接就回收掉了