下面这个情况会出现内存泄露吗?update方法里面要不要手动的把Map的数据清除先?
public class CacheData { private Map<String, List<ChannelState>> ipChMap = null; private static CacheData instance = new CacheData(); private CacheData() {
ipChMap = getData();
} public static CacheData getInstance() {
return instance;
} static private Map<String, List<ChannelState>> getData() {
Map<String, List<ChannelState>> temp = new HashMap<String, List<ChannelState>>();
List<ChannelState> csList = new ChannelStateDao().queryAll();
for (ChannelState cs : csList) {
String ip = cs.getIp();
List<ChannelState> ls = new ArrayList<ChannelState>();
ls.add(cs);
if (temp.containsKey(ip)) {
temp.get(ip).add(cs);
} else {
temp.put(ip, ls);
}
}
return temp;
} public List<ChannelState> getChannelStateByIp(String ip) {
List<ChannelState> ls = ipChMap.get(ip);
return ls;
} public void update() {
ipChMap = getData();
// memory leakage???
}
}
public class CacheData { private Map<String, List<ChannelState>> ipChMap = null; private static CacheData instance = new CacheData(); private CacheData() {
ipChMap = getData();
} public static CacheData getInstance() {
return instance;
} static private Map<String, List<ChannelState>> getData() {
Map<String, List<ChannelState>> temp = new HashMap<String, List<ChannelState>>();
List<ChannelState> csList = new ChannelStateDao().queryAll();
for (ChannelState cs : csList) {
String ip = cs.getIp();
List<ChannelState> ls = new ArrayList<ChannelState>();
ls.add(cs);
if (temp.containsKey(ip)) {
temp.get(ip).add(cs);
} else {
temp.put(ip, ls);
}
}
return temp;
} public List<ChannelState> getChannelStateByIp(String ip) {
List<ChannelState> ls = ipChMap.get(ip);
return ls;
} public void update() {
ipChMap = getData();
// memory leakage???
}
}
可以直接使用ipChMap
update 方法是要去数据库更新一下数据,数据库的数据时不断变化的ipChMap就会直接赋给另外一个引用。。之前的引用还会存在内存中吗?(因为ipChMap里面还包含一个map,可能不会被释放)
虽然ipChMap已经没有引用,
其中的小map还有引用,它会不会被回收呢?