[英]Facing Problems while displaying entire hashmap - java out of heap space
public static void main(String args[]) throws FileNotFoundException, IOException
{
FileReader in = new FileReader("/home/aoblah/Downloads/file1.txt");
BufferedReader br = new BufferedReader(in);
// Scanner in = null;
String answer = null;
String verb = null;
String line = null;
String [] split_npn = null;
String [] split_pn = null;
String [] split_dp2 = null;
String [] split_dp3 = null;
HashMap<String, HashMap<String, ArrayList<String>>> hmap = new HashMap<String, HashMap<String, ArrayList<String>>>();
try {
while ((line = br.readLine()) != null)
{
if(line.contains("verb"))
{
verb = line.substring(0, line.indexOf("("));
if(line.contains("npn")){
String test = line.substring(line.indexOf("npn"));
answer = test.substring(test.indexOf("npn"),test.indexof(']'));
answer = answer.replace(",", " " );
split_npn = answer.split(" ");
}
if(line.contains("pn")){
String test = line.substring(line.indexOf("pn"));
answer = test.substring(test.indexOf("pn"), test.indexOf(']'));
answer = answer.replace(",", " " );
split_pn = answer.split(" ");
}
if(line.contains("dp2")){
String test = line.substring(line.indexOf("dp2"));
answer = test.substring(test.indexOf("dp2"), test.indexOf(']'));
answer = answer.replace(",", " " );
split_dp2 = answer.split(" ");
}
if(line.contains("dp3")){
String test = line.substring(line.indexOf("dp3"));
answer = test.substring(test.indexOf("dp3"), test.indexOf(']'));
answer = answer.replace(",", " " );
split_dp3 = answer.split(" ");
}
}
if(split_npn != null){
ArrayList<String> npn = new ArrayList<String>();
hmap.put(verb, new HashMap<String, ArrayList<String>>());
for(int i = 1; i< split_npn.length; i++){
npn.add(split_npn[i]);
}
npn.trimToSize();
hmap.get(verb).put("npn", npn);
}
if(split_pn != null){
ArrayList<String> pn = new ArrayList<String>();
hmap.put(verb, new HashMap<String, ArrayList<String>>());
for(int i = 1; i< split_pn.length; i++){
pn.add(split_pn[i]);
}
pn.trimToSize();
hmap.get(verb).put("pn", pn);
}
if(split_dp2 != null){
ArrayList<String> dp2 = new ArrayList<String>();
hmap.put(verb, new HashMap<String, ArrayList<String>>());
for(int i = 1; i< split_dp2.length; i++){
dp2.add(split_dp2[i]);
}
dp2.trimToSize();
hmap.get(verb).put("dp2", dp2);
}
if(split_dp3 != null){
ArrayList<String> dp3 = new ArrayList<String>();
hmap.put(verb, new HashMap<String, ArrayList<String>>());
for(int i = 1; i< split_dp3.length; i++){
dp3.add(split_dp3[i]);
}
dp3.trimToSize();
hmap.get(verb).put("dp3", dp3);
}
System.out.println(hmap);
}
}
catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The file contains entries such as该文件包含条目,例如
calm(verb,naux,[nullobj,[dp2,down],[dp3,down]],calm).平静(动词,诺克斯,[nullobj,[dp2,向下],[dp3,向下]],平静)。
calms(verb,[sg],calm).平静(动词,[sg],平静)。
calmed(verb,[sg,pl,en],calm).平静(动词,[sg,pl,en],平静)。
calm(verb,[pl,inf],calm).平静(动词,[pl,inf],平静)。
calming(verb,[ing],calm).平静(动词,[ing],平静)。
calmly(adv,mv,calmly).平静地(adv,mv,平静地)。
calmness(noun,[sg],calmness).平静(名词,[sg],平静)。
I am storing all the verbs, categories of the prepositions associated with it and the prepositions in a nested hash map.我将所有动词、与之相关的介词类别以及嵌套哈希图中的介词存储起来。 I have no trouble when printing only a particular category of prepositions associated with the verb, but when I display the whole hashmap the programs just runs and never stops...ending up in java out of space heap error.
当我只打印与动词相关的特定类别的介词时,我没有问题,但是当我显示整个哈希图时,程序只是运行并且永不停止......最终在 java 空间不足堆错误中结束。 How can i fix this memory problem?
我该如何解决这个内存问题?
One suggestion is that instead of doing System.out.println(hmap);一个建议是不要做 System.out.println(hmap);
Try to print it out sequentually:尝试按顺序打印出来:
for(String outerKey: hmap.keySet())
{
System.out.println(outerKey + ": ");
Map inner = hmap.get(outerKey);
for(String innerKey : innerMap)
{
System.out.print(innerKey + ": ");
System.out.println(innterMap.get(innerKey).toString());
}
}
You print the map using您使用打印地图
System.out.println(hmap)
This uses the toString()
method of the HashMap
which build one large string in memory for the whole output.这使用了
HashMap
的toString()
方法,该方法在内存中为整个输出构建一个大字符串。 Depending on the size of you input file read this can be much memory.根据您读取的输入文件的大小,这可能需要很多内存。
It would be better to print the map iterating the entries:最好打印迭代条目的地图:
for (Object e : hmap.entrySet()) {
System.out.print(e);
}
System.out.println();
This will allocate memory for one entry, print it, hand it to the garbage collector and take the next.这将为一个条目分配内存,打印它,将其交给垃圾收集器并获取下一个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.