Using Oracle java JDBC (ojdbc6 11.2.0.4), loading a query with many rows takes forever (high latency environment. This is apparently the default prefetch in Oracle JDBC is default size "10" which requires a round trip time once per 10 rows. I am attempting to set an aggressive prefetch size to avoid this.

 PreparedStatement stmt = conn.prepareStatement("select * from tablename");
statement.setFetchSize(10000);
ResultSet rs = statement.executeQuery();

This can work, but instead I get an out of memory exception. I had presumed that setFetchSize would tell it to buffer "that many rows" as they come in, using as much RAM as each row requires. If I run with 50 threads, even with 16G of -XMX space, it runs out of memory. Feels almost like a leak:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.lang.reflect.Array.newArray(Native Method)
at java.lang.reflect.Array.newInstance(Array.java:70)
at oracle.jdbc.driver.BufferCache.get(BufferCache.java:226)
at oracle.jdbc.driver.PhysicalConnection.getCharBuffer(PhysicalConnection.java:7422)
at oracle.jdbc.driver.OracleStatement.prepareAccessors(OracleStatement.java:983)
at oracle.jdbc.driver.T4CTTIdcb.receiveCommon(T4CTTIdcb.java:273)
at oracle.jdbc.driver.T4CTTIdcb.receive(T4CTTIdcb.java:144)
at oracle.jdbc.driver.T4C8Oall.readDCB(T4C8Oall.java:771)
at oracle.jdbc.driver.T4CTTIfun.receive(T4CTTIfun.java:346)
at oracle.jdbc.driver.T4CTTIfun.doRPC(T4CTTIfun.java:186)
at oracle.jdbc.driver.T4C8Oall.doOALL(T4C8Oall.java:521)
at oracle.jdbc.driver.T4CPreparedStatement.doOall8(T4CPreparedStatement.java:205)
at oracle.jdbc.driver.T4CPreparedStatement.executeForDescribe(T4CPreparedStatement.java:861)
at oracle.jdbc.driver.OracleStatement.executeMaybeDescribe(OracleStatement.java:1145)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1267)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3449)
at oracle.jdbc.driver.OraclePreparedStatement.executeQuery(OraclePreparedStatement.java:3493)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.executeQuery(OraclePreparedStatementWrapper.java:1491)
....

What can I do to still get prefetch but not run out of RAM? What is going on?

The closest related item on SO is this: http://stackoverflow.com/a/14317881/32453

解决方法

Basically, oracle's default strategy for later ojdbc jars is to "pre allocate" an array per "prefetch" row that accomodates for the largest size conceivably possible to return from that query. So in my case I had some VARCHAR2(4000) in there, so 50 threads * 3 columns of varchar2's * 4000 was adding up to more than gigabytes of RAM [yikes]. There does not appear to be an option to say "don't pre allocate that array, just use the size needed." Ojdbc even keeps these preallocated buffers around between preparedstatements so it can reuse them. Definitely a memory hog.

The fix was to determine the maximum actual column size, then replace the query with (assuming 50 is the max size) select substr(column_name, 0, 50) as well as profile and only use as high of setFetchSize as actually made significant speed 
 ments.

Other things you can do: decrease the number of prefetch rows, increase Xmx parameter, only select the columns you need.

Once we were able to use at least prefetch 400 [make sure to profile to see what numbers are good for you, with high latency we saw 
 ments up to prefetch size 3-4K] on all queries, performance 
 d dramatically.

I suppose if you wanted to be really aggressive against sparse "really long" rows you might be able to re-query when you run into these [rare] rows.

Details ad nauseum here

最新还有个解决方法,使用ojdbc8,已经是按需分配模式。

Oracle JDBC prefetch: how to avoid running out of RAM的更多相关文章

  1. Ignite 配置更新Oracle JDBC Drive

           如果使用Oracle 12C 作为Ignite 的Repository的话,在Repository Createion Wizard的配置过程中,会出现ORA-28040:No matc ...

  2. created a ThreadLocal with key of type [oracle.jdbc.driver.AutoKeyInfo$1]

    环境: spring4.3, mybatis3.5.2, ojdbc8_8c(oracle 18c jdbc) 调试状态下退出时,提示: 严重 [main] org.apache.catalina.l ...

  3. 高性能 Oracle JDBC 编程

    了解如何利用连接和语句池特性来提高 Oracle 驱动的 JDBC 程序的性能.作者:Yuli Vasiliev2009 年 4 月发布使用诸如连接池和语句池等池技术可以显著提高数据库密集型应用程序的 ...

  4. java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver 错误的解决办法

    java.lang.ClassNotFoundException: oracle.jdbc.driver.OracleDriver 错误的解决办法 (2011-05-05 16:08:05) 转载▼ ...

  5. mysql和oracle jdbc连接

    加载驱动. Class.forName("oracle.jdbc.driver.OracleDriver"); 1 创建连接. Connection con = DriverMan ...

  6. Eclipse中启用Oracle jdbc logging

    根据自己用的JRE版本, jre1.5选择ojdbc5_g.jar, jre6选择ojdbc6_g.jar, 只有带_g的dirver才输出debug信息. maven 的配置信息: <depe ...

  7. 在MAVEN仓库中添加ORACLE JDBC驱动

    本文转载自 http://www.cnblogs.com/leiOOlei/archive/2013/10/21/3380568.html 因为已经是第二次遇到,所以COPY过来,怕以后别人的BLOG ...

  8. 连接oracle jdbc

    我使用的是精简版的oracle. 1  导入oracle驱动包 oracle下路径 D:\oracle\app\oracle\product\11.2.0\server\jdbc\lib\ojdbc6 ...

  9. maven oracle jdbc jar

    1.problem describe: when your dependency jar about oracle use code like this: <!-- oracle-connect ...

随机推荐

  1. 汽配生产的精益化管理如何实现?这家3000人的企业靠MES系统进行管理

    精益达电子事业部电子车间于在完成车间改造后,生产能力得到大幅提升.但生产制造过程信息化仍处于空白,众多设备处于单机工作模式,车间现场计划排产.物料管理.质量管理等,还处于原始的凭经验.人工干预方式. ...

  2. xenserver增加新硬盘

    1. 查看所有硬盘了的id # ls -l /dev/disk/by-id/ 2. 挂载 # xe sr-create type=lvm content-type=user device-config ...

  3. MySQL安装和使用

    1.MySQL安装: 1).到MySQL官网https://dev.mysql.com/downloads/installer/,下载MySQL 5.7版本:(注:现在官网上最新版本已经变成8.0.1 ...

  4. Golang: 常用的文件读写操作

    Go 语言提供了很多文件操作的支持,在不同场景下,有对应的处理方式,今天就来系统地梳理一下,几种常用的文件读写的形式. 一.读取文件内容 1.按字节读取文件 这种方式是以字节为单位来读取,相对底层一些 ...

  5. kill 命令在Java应用中使用注意事项

    前言 我们都知道,kill在linux系统中是用于杀死进程. kill pid [..] kill命令可将指定的信号发送给相应的进程或工作. kill命令默认使用信号为15,用于结束进程或工作.如果进 ...

  6. JAVA分页工具类

    最近写了一个代码生成工具,分享下该工具下的分页工具 一.分页工具类 package com.qy.code.api.page; import java.io.Serializable; import ...

  7. 【Nginx】使用certbot安装免费https证书使Nginx支持Https请求

    certbot官网:https://certbot.eff.org/lets-encrypt/centosrhel7-nginx 一.安装步骤 1)安装certbot,执行  sudo yum ins ...

  8. restframework详细

    1.写视图的方法 1.1第一种:原始APIView url(r'^login/$',account.LoginView.as_view()), from rest_framework.views im ...

  9. lvs+keepalived高可用负载均衡

    一.实验环境和网络拓扑图 本实验需要5台虚拟机,一台客户机,2台lvs调度器,两台web服务器. 客户机:192.168.0.6/24 lvs1:192.168.0.201/24 lvs2:192.1 ...

  10. 【转】大众点评Cat--架构分析

    https://blog.csdn.net/szwandcj/article/details/51025669 Cat功能强大且多,光日志的报表和图表分析就有十几种,但文档却很少,寥寥无几找到一些粒度 ...