public class ETFArbitrageRateCache implements ExtendedRefreshService {
private static ETFArbitrageRateCache cache;
private HashMap pool = new HashMap();
private ETFArbitrageRateCache() {
refresh(0);
}
private ETFArbitrageRateCache(HashMap newPool) {
pool = newPool;
}
public static ETFArbitrageRateCache getInstance() {
if (cache == null) {
cache = new ETFArbitrageRateCache();
}
return cache;
}
public boolean refresh() {
return refresh(1);
}
private boolean refresh(int refreshType) {
TransactionManager transMgr = null;
boolean successFlag = true;
try {
transMgr = new TransactionManager(PoolType.SPEC_POOL);
DBETFArbitrageRate db = new DBETFArbitrageRate(transMgr);
HashMap newPool = new HashMap();
transMgr.beginTransaction();
String[] orders =
new String[]{"currencyId", "acctId", "exchId", "fundCode", "fluctuateType"};
db.setOrders(orders);
db.select(CommData.ALL_RECORDS);
while (db.next()) {
ETFArbitrageRateRWValue value = new ETFArbitrageRateRWValue();
value.setCurrencyId(db.getCurrencyId());
value.setAcctId(db.getAcctId());
value.setExchId(db.getExchId());
value.setFundCode(db.getFundCode());
value.setFluctuateType(db.getFluctuateType());
value.setStkRate1(db.getStkRate1());
value.setStkRate2(db.getStkRate2());
value.setStkRate3(db.getStkRate3());
value.setStkRate4(db.getStkRate4());
value.setStkRate5(db.getStkRate5());
value.setStkRate6(db.getStkRate6());
value.setFundRate1(db.getFundRate1());
value.setFundRate2(db.getFundRate2());
value.setFundRate3(db.getFundRate3());
value.setFundRate4(db.getFundRate4());
value.setFundRate5(db.getFundRate5());
value.setFundRate6(db.getFundRate6());
value.setBeginOrderRate(db.getBeginOrderRate());
value.setEndOrderRate(db.getEndOrderRate());
newPool.put(value.getPrimaryKey(), value);
}
transMgr.endTransaction();
if (refreshType == 0)
pool = newPool;
else {
synchronized (cache) {
cache = new ETFArbitrageRateCache(newPool);
}
}
} catch (Throwable e) {
if (transMgr != null)
transMgr.cancelTransaction();
successFlag = false;
Log.log(e);
}
return successFlag;
}
如上代码,内存在多次刷新之后,ETFArbitrageRateCache和ETFArbitrageRateCache.ETFArbitrageRateRWValue会呈几何增长,在jprofiler中使用GC回收也不起作用。将程序代码稍作修改:
if (refreshType == 0)
pool = newPool;
else {
synchronized (cache) {
pool.clear();
cache = new ETFArbitrageRateCache(newPool);
}
}
这时,value的释放会加快,多次刷新后,有时候jvm会自动回收掉,调用jprofiler的GC,发现空闲的value全部都被释放,但是ETFArbitrageRateCache还有两个。
再修改一下代码:
synchronized (pool) {
pool = newPool;
}
这时ETFArbitrageRateCache不会增长,value在调用GC后也能及时回收。
不知道是不是类变量释放引用之后,被引用的对象中如果还有大量的对象,这时该对象不能被GC回收,先将此报告放在这,待以后细究,有对此深究过的大侠也可以发表一下看法!!!!