我正在尝试使用以下代码段从Sun Identity Manager数据库中读取XML文件详细信息
String xmlTxt="";
ArrayList<String> xmlList = new ArrayList<String>();
BLOB blob=null;
OracleConnection conn=null;
OraclePreparedStatement stmt = null;
OracleResultSet rs = null;
GZIPInputStream gStream = null;
log.debug("Initializing DB connection...");
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
conn=(oracle.jdbc.OracleConnection)DriverManager.getConnection(dbURL, dbUserName, dbPassword);
stmt = (OraclePreparedStatement) conn.prepareCall("SELECT XML FROM TASK WHERE TYPE='WorkItem'");
rs = (OracleResultSet) stmt.executeQuery();
while(rs.next()){
blob = rs.getBLOB(columnLabel);
if(!blob.isEmptyLob())
gStream = new GZIPInputStream(blob.getBinaryStream());
if(gStream.available()>0)
xmlTxt = IOUtils.toString(gStream,columnEncoding);
xmlList.add(xmlTxt);
}
}
catch(ClassNotFoundException cnf){
//Handle errors for Class.forName
log.error("ClassNotFoundException " + cnf.getMessage());
log.error("ClassNotFoundException " + cnf);
}
catch (SQLException se) {
//Handle errors for JDBC
log.error("SQLException " +se.getMessage());
log.error("SQLException " + se);
}
catch (IOException ie) {
//Handle errors for I/O
log.error("IOException " +ie.getMessage());
log.error("IOLException " + ie);
}
catch (OutOfMemoryError E){
log.error("OutOfMemoryError Encountered :"+ Runtime.getRuntime().totalMemory());
}
finally{
try {
if(rs!=null)
rs.close();
if(stmt!=null)
stmt.close();
if(blob!=null)
blob.free();
if(conn!=null)
conn.close();
}
catch (SQLException se) {
//Handle errors for JDBC
log.error("SQLException during close " +se.getMessage());
log.error("SQLException during close" + se);
}
}
log.debug("End of DB Operation");
return xmlList;
目前我正在Dev环境中对其进行测试,其中记录为100,每个blob数据的平均大小为2 MB。此外,当总记录稳定增加时,我可以看到堆大小的消耗量很大。我当前的Java堆大小是Xmx512m -Xms64m,在prod中我们有近150万条记录需要处理,所以想知道需要多少堆大小。
我通常会在下线
时出现内存错误xmlTxt = IOUtils.toString(gStream,columnEncoding);
https://commons.apache.org/proper/commons-io/apidocs/org/apache/commons/io/IOUtils.html
请告知我们是否有其他更好的方法可以优化代码以提高性能。
答案 0 :(得分:0)
以流方式解析表中的数据,而不是将其作为字符串/数组列表复制到内存中。