简体   繁体   English

Java内存不足异常(jdbc)

[英]java out of memory Exception (jdbc)

I am trying to read serialized objects from mysql database in a loop and perform some operations on it in java. 我试图在循环中从mysql数据库读取序列化的对象,并在java中对其执行一些操作。 I have written the following function for returning me the object from ResultSet the object. 我编写了以下函数,用于从ResultSet对象返回我该对象。

public static MyObj deSerializeCacheTagInfo(ResultSet res
    ) throws SQLException, IOException, ClassNotFoundException 
{
    byte[] buf = res.getBytes(3);
    ObjectInputStream objectIn = null;
    if (buf != null)
        objectIn = new ObjectInputStream(new ByteArrayInputStream(buf));
    MyObj info = (MyObj)objectIn.readObject();
    return info;
}

When I run this code, it gives me an out of memory exception. 当我运行此代码时,它给了我内存不足的异常。 I searched around a bit and realized it could be because result set is large and it is kept in memory, so I tried fetching some 50 rows at a time. 我搜索了一下,发现可能是因为结果集很大并且保存在内存中,所以我尝试一次获取约50行。

But that doesn't seem to help either. 但这似乎也无济于事。

On profiling with visualvm, it reports that all the space is being hogged by byte[] objects . 使用visualvm进行分析时,它报告所有空间都被byte[] objects占用。 But I am not entirely sure what's going wrong. 但是我不确定是怎么回事。

By default MySQL JDBC driver fetches the complete ResultSet into memory. 默认情况下,MySQL JDBC驱动程序将完整的ResultSet提取到内存中。

You can change this to a streamed fetch with something like this: 您可以将其更改为具有以下内容的流式获取:

Statement st = connIn.createStatement(ResultSet.TYPE_FORWARD_ONLY, ResultSet.CONCUR_READ_ONLY);
st.setFetchSize(Integer.MIN_VALUE);

The combination of a forward-only, read-only result set, with a fetch size of Integer.MIN_VALUE serves as a signal to the driver to stream result sets row-by-row. 前向只读结果集与访存大小为Integer.MIN_VALUE的组合向驱动程序发出信号以逐行传输结果集。 After this any result sets created with the statement will be retrieved row-by-row. 此后,将使用该语句创建的任何结果集逐行检索。

Try to add objectIn.close(); 尝试添加objectIn.close(); before returning, it may help 返回之前,可能会有所帮助

You can increase the heap space by using the option -mx256m for 256mb heap space or -mx512m for 512mb and so on. 您可以通过使用-mx256m选项-mx256m用于256mb堆空间)或-mx512m用于512mb)来增加堆空间。

Search for increasing heap space by setting VM arguments on net. 通过在网络上设置VM参数来搜索增加的堆空间。

this might help 这可能会有所帮助

Increase heap size in Java 增加Java中的堆大小

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM