数据库连接池持续运行2小时,通过JConsle观察内存使用情况是否持续上升。正常情况,上升一段时间后就会下降。若一直上升,则有可能存在内存泄漏的情况。结合jmap\jstack可分析内存、对象的情况。
package com.puti;
import
java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import com.puti._utillog.JOSLogger;
import com.puti.webos.dbpool.ConnectionPool;
import com.puti.webos.dbpool.PoolManager;
public class Test
{
static JOSLogger logger = JOSLogger.getLogger(Test.class);
public static void main(String[] args) throws Exception
{
logger.info("--------------------");
int i=0;
while(true)
{
i++;
asynchronizeCloseConnection();
if(i==500)
{
i=0;
Thread.sleep(1000);
}
}
}
public static void memoryLeakTest()
{
ConnectionPool pool = null;
Connection conn = null;
Statement stmt = null;
Runtime runtime = Runtime.getRuntime();
System.out.println(runtime.freeMemory()/1024);
System.out.println(runtime.maxMemory()/1024);
long start = 0l;
long end = 0l;
start = System.currentTimeMillis();
try
{
pool = PoolManager.getPool("sybase");
conn = pool.getConnection();
stmt = conn.createStatement();
stmt.executeQuery("select top 50 * from jos.dbo.T_JOS_TEST");
stmt.close();
conn.close();
}
catch(Exception e)
{
if(conn!=null)
try {
conn.close();
} catch (SQLException e1) {
e1.printStackTrace();
}
e.printStackTrace();
}
end = System.currentTimeMillis();
System.err.println("cost time:" + (end - start) / 1000 +"s");
}
private static void asynchronizeCloseConnection()
{
Runnable _closeRunable = new Runnable()
{
public void run()
{
memoryLeakTest();
}
};
;
new Thread(_closeRunable).start();
}
}