I'm trying to read a file and save the lines which share the same first token (readId) in a set(of String). Each set is part of my hashmap >.
I already increased my heap to 32 giga, also move from string.split to StringTokenizer, but still I am having this error:
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOfRange(Arrays.java:2694)
at java.lang.String.<init>(String.java:203)
at java.lang.String.substring(String.java:1913)
at java.util.StringTokenizer.nextToken(StringTokenizer.java:352)
at java.util.StringTokenizer.nextElement(StringTokenizer.java:407)
at Simple1_BootStrap.createMapSet(Simple1_BootStrap.java:68)
at Simple1_BootStrap.main(Simple1_BootStrap.java:206)
Previously, the "out of memory error" was generated by this line:
Set<String> s =new TreeSet<String>();
The piece of the code producing the error is:
Map<String,Set<String>> map2 = new HashMap<String,Set<String>>();
try{
BufferedReader br = new BufferedReader(new FileReader(filename));
String strLine;
String readId;
while ((strLine = br.readLine()) != null) {
alignment ++;
StringTokenizer stringTokenizer = new StringTokenizer(strLine);
readId = stringTokenizer.nextElement().toString();
if(map2.containsKey(readId)) {
Set<String> s = map2.get(readId);
s.add(strLine);
map2.put(readId, s);
}
else {
Set<String> s =new TreeSet<String>();
s.add(strLine);
map2.put(readId, s);
}
}
br.close();
}catch (Exception e){//Catch exception if any
System.err.println("Error: " + e.getMessage());
}
I put those lines inside a set because I need to randomly select entries in my hashmap and read the associated set to create a file similar to the input file.
Could somebody pls suggest another approach to avoid the "out of memory error"?
Thank you.