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. 单词diamaund钻石diamaund英文

    Diamond Di"a*mond (?; 277), n. [OE. diamaund, the hardest iron, steel, diamond, Gr. . Perh. the ...

  2. CSS 样式的使用方式、选择器

    在html中使用css的三种方式: 1.行内样式:同过元素的style属性来设置 <p style="font-size:20px; color:red">hello& ...

  3. caffe库源码剖析——net层

    net层的功能实现主要涉及到net.hpp和net.cpp文件,让我们要捋顺它是干了什么,是如何实现的. 1. net层使用到的参数 第一步要做的事,就是查看caffe.proto文件,弄清楚net都 ...

  4. java虚拟机类加载机制和双亲委派模型

    java虚拟机类加载机制:虚拟机把描述类的数据从class文件加载到内存,并对数据进行校验.转换解析和初始化,最终形成可以被虚拟机直接使用的java类型. 类的生命周期是从类被加载到虚拟机内存中,到卸 ...

  5. U盘出现.exe问题的解决方案

    这代表U盘中了AutoRun病毒,原文件并未被删除,只是被设置为隐藏了. 所以切勿直接格式化U盘! 解决方案:分别创建文件1.reg和2.bat: 1.reg Windows Registry Edi ...

  6. Shell 编程 基础

    本篇主要写一些shell脚本的基础知识,编程规范. 第一个shell脚本 [root@localhost ~]# vim first.sh #!/bin/bash # This is first Sh ...

  7. win10 + Ubuntu18.04 双系统,UEFI+GPT,从win10切换到Ubuntu时黑屏问题

    1.现象: ①win10主系统,从win10重启,立即黑屏,之后会进入Ubuntu(还是黑屏)(为什么会知道进入了Ubuntu:按音量键可以听到Ubuntu音量加减的系统声音,数字锁定和大小写锁定均有 ...

  8. 【Git】Git如何合并某一次commit的内容到指定分支

    一.我是在什么场景下会用到该Git操作 当某同事,将开发分支dev2合并到开发分支dev1时(两个不同的功能,不能合并),其他同事不知情的情况下,继续在dev1上开发并提交了代码. 后面发现了该合并, ...

  9. Httpd服务入门知识-Httpd服务常见配置案例之基于客户端来源地址实现访问控制

    Httpd服务入门知识-Httpd服务常见配置案例之基于客户端来源地址实现访问控制 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Options  1>.OPTIONS指 ...

  10. 图论 - 图的深度优先遍历c++实现

    图的深度优先遍历c++实现 深度优先搜索 邻接矩阵的创建 int i, j, m, a, b; cin >> n >> m; //初始化二维矩阵 for (i = 1; i & ...