@TOC
問題背景
使用google.common的guava依賴的partition分割產(chǎn)生的淺拷貝問題,如:
把userList集合分割成每個(gè)10000的小集合
List<List<User>> partitions = Lists.partition(userList, 10000);
在老年代中會(huì)越來越到,知道產(chǎn)生Full GC
如果直接partitions.get(0).clear() 或者 userList.clear() 都會(huì)導(dǎo)致原數(shù)據(jù)的丟失,因?yàn)檫@是淺拷貝的方式
解決方案
List<List<User>> partitions = Lists.partition(userList, 10000);
每次調(diào)用的時(shí)候進(jìn)行深拷貝,使每個(gè)集合與原大集合partitions沒有關(guān)系,這是clear就沒有問題了
for (List<User> partition : partitions) {
//進(jìn)行深拷貝,使每個(gè)集合與原大集合partitions沒有關(guān)系,這是clear就沒有問題了
doMethod(new ArrayList<>(partition ));
}
partitions.clear();
userList.clear();
作為程序員第 244 篇文章,每次寫一句歌詞記錄一下,看看人生有幾首歌的時(shí)間,wahahaha ...


