java 板


LINE

我在执行下面程式的时候总是会发生heap记忆体不足的问题 主要会耗记忆体的部分我想应是下面两个Map static HashMap<Integer, HashMap> trustMap = new HashMap(); static HashMap<Integer, HashMap> tempMap = new HashMap(); 这两个MAP互相的关系为 利用tempMap的结果去更新trustMap 由於每次更新trustMap中的一个element,所以更新完成後会进行深层复制 把新的trustMap复制到tempMap并再继续进行下一步 这两个Map的结构大致如下 {key:integer,value:hashMap{key:integer,value:integer}} 是一个二维的map 以下是我的程式 希望能照找出我到底哪里浪费了记忆体 P.S.我用的tool是Eclipse 在run的设定中把记忆体调大我有试过 但依然还是记忆体不足 谢谢 import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.HashMap; import java.util.Iterator; import java.util.LinkedList; import java.util.ListIterator; import java.util.Map; import java.util.Set; import java.util.HashMap; public class TrustCF { String lastUserId; int dataNumber; static HashMap<Integer, HashMap> trustMap = new HashMap(); static HashMap<Integer, HashMap> tempMap = new HashMap(); static HashMap<Integer, Boolean> computeJumper = new HashMap(); int startUserId; static public void main(String args[]) throws IOException { TrustCF cc = new TrustCF("Car_user_trust.txt", 14474); cc.trainingTrustArray(10); System.out.println(cc.getTrustArray(1, 3)); } TrustCF(String sFileName, int userNum) throws IOException { dataNumber = 0; String line = null; BufferedReader br = null; BufferedReader br2 = null; String[] lineElements, tempLineElements = new String[10]; HashMap<Integer, Integer> TempTrustData = new HashMap(); try { br = new BufferedReader(new FileReader(sFileName)); br2 = new BufferedReader(new FileReader(sFileName)); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } lastUserId = br2.readLine().split("::")[0]; System.out.println("User ID starts at:" + lastUserId); startUserId = Integer.parseInt(lastUserId); tempLineElements[0] = "0"; while ((line = br.readLine()) != null) { dataNumber++; lineElements = line.split("::"); if (lineElements[0].compareTo(lastUserId) == 0) { TempTrustData.put(Integer.parseInt(lineElements[1]), 1); } else { trustMap.put(Integer.parseInt(lastUserId), TempTrustData); tempLineElements[0] = lineElements[0]; tempLineElements[1] = lineElements[1]; TempTrustData = new HashMap(); } if (Integer.parseInt(tempLineElements[0]) > 0) { TempTrustData.put(Integer.parseInt(lineElements[1]), 1); /* * System.out.println("Item ID: " + lineElements[1] + " User * Preference: " + lineElements[2]); */ tempLineElements[0] = "0"; // System.out.println(Integer.parseInt(lineElements[0])); } lastUserId = lineElements[0]; } trustMap.put(Integer.parseInt(lastUserId), TempTrustData); System.out.println("Finished reading trust file:" + sFileName); System.out.println("Numbre of trust records=" + trustMap.size()); System.out.println("Numbre of users=" + lastUserId); System.out.println("Numbre of datas=" + dataNumber); System.out .println("=============================================================="); for (int i = 0; i < userNum; i++) { if (trustMap.get(i + 1) != null) { // System.out.println(trustMap.get(i + 1)); if (get(trustMap, i + 1, i + 1) == 0) { ((HashMap) trustMap.get(i + 1)).put(i + 1, 1); } } else { HashMap hm1 = new HashMap(); hm1.put(i + 1, 1); trustMap.put(i + 1, hm1); } } DeepCopy dc = new DeepCopy(); System.out.println(trustMap); tempMap = (HashMap) dc.copy(trustMap); } static int get(HashMap map, int i, int j) { // System.out.println(i+","+j+map.get(i)); if (((HashMap) map.get(i)).get(j) != null) return Integer.parseInt(((HashMap) map.get(i)).get(j).toString()); return 0; } HashMap getTrustArray(int nActiveUser, int distance) { HashMap<Integer, Float> resultMap = new HashMap(); HashMap<Integer, Float> map = tempMap.get(nActiveUser); Set set = map.entrySet(); Iterator iterator = set.iterator(); int key; float value; while (iterator.hasNext()) { Map.Entry mapentry = (Map.Entry) iterator.next(); key = Integer.parseInt(mapentry.getKey().toString()); value = Float.parseFloat(mapentry.getValue().toString()); if (value <= distance) { value = (distance - value + 1) / distance; resultMap.put(key, value); } } return resultMap; } /** d在此为总构需要计算的最大深度 */ public static void trainingTrustArray(int d) { int n = 2; // Vector m1 = (Vector)all.clone();//复制物件 // System.out.println(originalTrustMap); // System.out.println(newTrustMap ); do { System.out.println("training" + d + "," + n); compute(n); n++; } while (n < d + 1); System.out.println("Numbre of trust=" + getInformationNum(tempMap)); } static int getInformationNum(Map map) { int result = 0; Set set = map.entrySet(); Iterator iterator = set.iterator(); while (iterator.hasNext()) { Map.Entry mapentry = (Map.Entry) iterator.next(); result += ((HashMap<Integer, HashMap>) map.get(mapentry.getKey())) .size(); } return result; } public static void compute(int n) { int count = 1; boolean flag = false; boolean jump = true; Set set1 = tempMap.entrySet(); Iterator iterator1 = set1.iterator(); int user1, user2, user3; while (iterator1.hasNext()) { if (count % 100 == 0) System.out.println(count); Map.Entry mapentry1 = (Map.Entry) iterator1.next(); user1 = Integer.parseInt(mapentry1.getKey().toString()); //if (computeJumper.get(user1) == null) { Set set2 = ((HashMap) tempMap.get(user1)).entrySet(); Iterator iterator2 = set2.iterator(); while (iterator2.hasNext()) { Map.Entry mapentry2 = (Map.Entry) iterator2.next(); user2 = Integer.parseInt(mapentry2.getKey().toString()); if (tempMap.get(user2) != null) { Set set3 = ((HashMap) tempMap.get(user2)).entrySet(); Iterator iterator3 = set3.iterator(); while (iterator3.hasNext()) { Map.Entry mapentry3 = (Map.Entry) iterator3.next(); user3 = Integer.parseInt(mapentry3.getKey() .toString()); if (((HashMap) trustMap.get(user1)).get(user3) == null) { ((HashMap) trustMap.get(user1)).put(user3, n); flag = true; jump = false; } } } if (jump == true) { computeJumper.put(user1, jump); jump = false; } //} } count++; } //System.out.println(computeJumper); if (flag = true) { DeepCopy dc = new DeepCopy(); // System.out.println(tempMap); tempMap.clear(); tempMap = (HashMap) dc.copy(trustMap); // System.out.println(tempMap); flag = false; } System.out.println(trustMap); } } --



※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 61.62.85.205







like.gif 您可能会有兴趣的文章
icon.png[问题/行为] 猫晚上进房间会不会有憋尿问题
icon.pngRe: [闲聊] 选了错误的女孩成为魔法少女 XDDDDDDDDDD
icon.png[正妹] 瑞典 一张
icon.png[心得] EMS高领长版毛衣.墨小楼MC1002
icon.png[分享] 丹龙隔热纸GE55+33+22
icon.png[问题] 清洗洗衣机
icon.png[寻物] 窗台下的空间
icon.png[闲聊] 双极の女神1 木魔爵
icon.png[售车] 新竹 1997 march 1297cc 白色 四门
icon.png[讨论] 能从照片感受到摄影者心情吗
icon.png[狂贺] 贺贺贺贺 贺!岛村卯月!总选举NO.1
icon.png[难过] 羡慕白皮肤的女生
icon.png阅读文章
icon.png[黑特]
icon.png[问题] SBK S1安装於安全帽位置
icon.png[分享] 旧woo100绝版开箱!!
icon.pngRe: [无言] 关於小包卫生纸
icon.png[开箱] E5-2683V3 RX480Strix 快睿C1 简单测试
icon.png[心得] 苍の海贼龙 地狱 执行者16PT
icon.png[售车] 1999年Virage iO 1.8EXi
icon.png[心得] 挑战33 LV10 狮子座pt solo
icon.png[闲聊] 手把手教你不被桶之新手主购教学
icon.png[分享] Civic Type R 量产版官方照无预警流出
icon.png[售车] Golf 4 2.0 银色 自排
icon.png[出售] Graco提篮汽座(有底座)2000元诚可议
icon.png[问题] 请问补牙材质掉了还能再补吗?(台中半年内
icon.png[问题] 44th 单曲 生写竟然都给重复的啊啊!
icon.png[心得] 华南红卡/icash 核卡
icon.png[问题] 拔牙矫正这样正常吗
icon.png[赠送] 老莫高业 初业 102年版
icon.png[情报] 三大行动支付 本季掀战火
icon.png[宝宝] 博客来Amos水蜡笔5/1特价五折
icon.pngRe: [心得] 新鲜人一些面试分享
icon.png[心得] 苍の海贼龙 地狱 麒麟25PT
icon.pngRe: [闲聊] (君の名は。雷慎入) 君名二创漫画翻译
icon.pngRe: [闲聊] OGN中场影片:失踪人口局 (英文字幕)
icon.png[问题] 台湾大哥大4G讯号差
icon.png[出售] [全国]全新千寻侘草LED灯, 水草

请输入看板名称,例如:Soft_Job站内搜寻

TOP