「bufferedreader hasnext」的推薦目錄:
- 關於bufferedreader hasnext 在 コバにゃんチャンネル Youtube 的精選貼文
- 關於bufferedreader hasnext 在 大象中醫 Youtube 的最佳貼文
- 關於bufferedreader hasnext 在 大象中醫 Youtube 的精選貼文
- 關於bufferedreader hasnext 在 [問題] heap space out of memory - 看板java - 批踢踢實業坊 的評價
- 關於bufferedreader hasnext 在 Reading lines with BufferedReader and checking for end of file 的評價
- 關於bufferedreader hasnext 在 BufferedReaderのIterableラッパー. 最終行の判定ができるので ... 的評價
- 關於bufferedreader hasnext 在 FastqReader (htsjdk 2.8.1 API) - Samtools 的評價
bufferedreader hasnext 在 大象中醫 Youtube 的最佳貼文
bufferedreader hasnext 在 大象中醫 Youtube 的精選貼文
bufferedreader hasnext 在 Reading lines with BufferedReader and checking for end of file 的推薦與評價
... <看更多>
bufferedreader hasnext 在 BufferedReaderのIterableラッパー. 最終行の判定ができるので ... 的推薦與評價
this(new BufferedReader(reader));. } @Override. public boolean hasNext() {. boolean result = false;. try {. if (!this.doneNext) {. this.line = this. ... <看更多>
bufferedreader hasnext 在 [問題] heap space out of memory - 看板java - 批踢踢實業坊 的推薦與評價
我在執行下面程式的時候總是會發生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
... <看更多>