I have a couple of question regarding this problem I am having. I read some other questions and I discovered that the problem is that I am generating hundred of thousands of small Maps (Map<mutableObject, String>
).
I did not get in the questions though, hy does this happen? I would like to understand what is happening behind the scenes. So If anyone has a pointer here it would be greatly appreciated.
The second question is related to what would be a good alternative to the use of HashMap. Each Map is different from each other but there are lots of repetitions of most of the elements. I am generating permutations of the Strings and I store them in maps. Does someone has a good solution and, if possible, also a pointer to understand why should I do it like that?
Thanks in advance
Altober
OK Here some code to those who requested. I am reading an XML file, I tried to reduce it to the minimal extent possible so you can have an idea of what is happening.
String s = null;
String wd = null;
SomeObject word = null;
List<Sentence> sentences = new ArrayList<Sentence>();
Sentence sentence = null;
String line = null;
while(in.hasNextLine()) {
line = in.nextLine().trim();
if((line.startsWith("<s>")) {
sentence = new Sentence();
senses = new HashMap<someObject, String>();
} else if(line.endsWith("</f>")) {
int beggin = line.indexOf('>');
wd = line.substring(beggin + 1, line.length() - 5);
word.word = wd;
if(s != null)
senses.put(word, s);
} else if if(line.endsWith("</s>")) {
sentence.setSenses(senses);
sentences.add(sentence);
}
}
SOLVED (here the solution)
After using using a memory analyser I figure out that the problem were not the maps but a enormous amount of strings that I was generating in the loop (each token had multiple attributes). So as the file was a result of string permutations there were a lot of repeated strings. I just used the .intetn()
for every string, as suggested in another post, and then it worked smoothly.