java - hibernate ScrollableResults OutOfMemory

标签 java mysql hibernate

我在 MySQL 数据库的表用户中有 7500 行,VM 选项:-Xmx8m 和下面的一些代码:

Query query = session.createQuery("select u from User u");
        ScrollableResults resultSet = query.setFetchSize(Integer.MIN_VALUE).setReadOnly(true).scroll(ScrollMode.FORWARD_ONLY);

        int i = 0;
        while (resultSet.next()) {
            User o = (User) resultSet.get(0);   
            System.out.println(o.getId());    
            i++;
            if (i % 50 == 0) {
                session.clear();
            }
        }
        resultSet.close();

但不幸的是,我的控制台中有以下输出: ... 5745 线程“main”中的异常 java.lang.OutOfMemoryError:超出 GC 开销限制。

但是我不明白为什么?? Integer.MIN_VALUE - 用作驱动程序逐行流式传输结果集的信号。也许我遇到了问题,因为 ScrollableResults 存储在内存中,而 8m 的堆空间不够??

最佳答案

当内存使用超出 Xmx 配置时。 JVM 可能会尝试收集垃圾以释放足够的内存,而垃圾收集器占用的时间过多。我们遇到内存不足异常“java.lang.OutOfMemoryError: GC overhead limit exceeded.”。你必须增加 (Xmx) 最大 JVM 内存量或只是删除 JVM 标志以使用默认分配。 (假设你有足够的物理内存)

Java HotSpot VM Options

Troubleshooting Memory Leaks

C:\>java -X

    -Xms<size>        set initial Java heap size
    -Xmx<size>        set maximum Java heap size

The -X options are non-standard and subject to change without notice.

因为这是一个小数据集 (7500),mysql JDBC 驱动程序应该能够处理它。请注意,不会从 session 中清除最后一批记录。即使来自 http://dev.mysql.com/doc/connector-j/en/connector-j-reference-implementation-notes.html在结果集部分:

By default, ResultSets are completely retrieved and stored in memory.In most cases this is the most efficient way to operate and, due to the design of the MySQL network protocol, is easier to implement. If you are working with ResultSets that have a large number of rows or large values and cannot allocate heap space in your JVM for the memory required, you can tell the driver to stream the results back one row at a time.

您必须检查 hibernate 和 JDBC 驱动器版本。

关于java - hibernate ScrollableResults OutOfMemory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28333168/

相关文章:

hibernate - 我在 tomcat 控制台中收到内存泄漏异常

mysql - @SQLResultSetMapping+Joins 在多个实体上的不同结果 |日本PA

java - JPA Criteria API 中的订单自定义 SQL

java - JUnit 测试通过但 PIT 说套件不是绿色的

java - java中链表的快速排序实现

php - 如何避免在mysql php中多次插入相同的记录

mysql - 如何在我的 mysql 表上进行更快的查询?

java - System.nanoTime() 是否保证返回唯一值?

java - JVM requirements incompatible 错误

mysql - 对返回多行的 SELECT 结果求和