gpt4 book ai didi

java - 如何克服大文件写入期间的 OutOfMemoryError

转载 作者:搜寻专家 更新时间:2023-11-01 02:54:49 25 4
gpt4 key购买 nike

我正在用 Java 编写一个完整的数据库提取程序。数据库是Oracle,而且很大。有些表有大约 2.6 亿条记录。该程序应该以特定格式为每个表创建一个文件,因此不能使用 Oracle 数据泵等。此外,一些公司的安全政策不允许为此要求编写 PL/SQL 程序在数据库服务器上创建文件。我必须使用 Java 和 JDBC。

我面临的问题是,由于某些表的文件很大(~30GB),即使使用 20GB 的 Java 堆,我几乎每次都会用完内存。在创建文件期间,当文件大小超过堆大小时,即使使用最激进的 GC 策略之一,该过程似乎也会挂起。例如,如果文件大小 > 20GB 并且堆大小为 20GB,一旦堆利用率达到最大堆大小,它就会减慢每分钟写入 2MB 左右的速度,以这种速度,将需要几个月的时间才能完全提取。

我正在寻找解决此问题的方法。任何帮助将不胜感激。

以下是我的系统配置的一些细节:Java-JDK1.6.0_14

系统配置 - RH Enterprise Linux (2.6.18) 在 4 X Intel Xeon E7450(6 核)@2.39GH 上运行

内存 - 32GB

数据库 Oracle 11g

包含部分代码的文件如下:

private void runQuery(Connection conn, String query, String filePath,
String fileName) throws SQLException, Exception {
PreparedStatement stmt = null;
ResultSet rs = null;
try {
stmt = conn.prepareStatement(query,
ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
stmt.setFetchSize(maxRecBeforWrite);
rs = stmt.executeQuery();
// Write query result to file
writeDataToFile(rs, filePath + "/" + fileName, getRecordCount(
query, conn));
} catch (SQLException sqle) {
sqle.printStackTrace();
} finally {
try {
rs.close();
stmt.close();
} catch (SQLException ex) {
throw ex;
}
}
}

private void writeDataToFile(ResultSet rs, String tempFile, String cnt)
throws SQLException, Exception {
FileOutputStream fileOut = null;
int maxLength = 0;
try {
fileOut = new FileOutputStream(tempFile, true);
FileChannel fcOut = fileOut.getChannel();

List<TableMetaData> metaList = getMetaData(rs);
maxLength = getMaxRecordLength(metaList);
// Write Header
writeHeaderRec(fileOut, maxLength);
while (rs.next()) {
// Now iterate on metaList and fetch all the column values.
writeData(rs, metaList, fcOut);
}
// Write trailer
writeTrailerRec(fileOut, cnt, maxLength);
} catch (FileNotFoundException fnfe) {
fnfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} finally {
try {
fileOut.close();
} catch (IOException ioe) {
fileOut = null;
throw new Exception(ioe.getMessage());
}
}
}

private void writeData(ResultSet rs, List<TableMetaData> metaList,
FileChannel fcOut) throws SQLException, IOException {
StringBuilder rec = new StringBuilder();
String lf = "\n";
for (TableMetaData tabMeta : metaList) {
rec.append(getFormattedString(rs, tabMeta));
}
rec.append(lf);
ByteBuffer byteBuf = ByteBuffer.wrap(rec.toString()
.getBytes("US-ASCII"));
fcOut.write(byteBuf);
}

private String getFormattedString(ResultSet rs, TableMetaData tabMeta)
throws SQLException, IOException {
String colValue = null;
// check if it is a CLOB column
if (tabMeta.isCLOB()) {
// Column is a CLOB, so fetch it and retrieve first clobLimit chars.
colValue = String.format("%-" + tabMeta.getColumnSize() + "s",
getCLOBString(rs, tabMeta));
} else {
colValue = String.format("%-" + tabMeta.getColumnSize() + "s", rs
.getString(tabMeta.getColumnName()));
}
return colValue;

最佳答案

这可能是由于您调用 prepareStatement 的方式所致, 请参阅 this question对于类似的问题。您不需要可滚动性,ResultSet 将默认为只读,因此只需调用

stmt = conn.prepareStatement(query);

关于java - 如何克服大文件写入期间的 OutOfMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3725861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com