I am retrieving some data from server for around 500 objects and storing it in a HashMap and when all the data for 500 objects have been collected, I used to write it to simple plain text files as Strings using BuffrerdWriter. But I was getting OutOfMemoryError only in the last thread.
So I decided to break-up data collection and rather than collecting data for 500 objects, I started collecting data for 50 objects at a time in a loop populating hashmap locally so that it can be garbage collected in the next iteration and kept BufferedWriter open for the whole time rather than opening and closing it in the end at once.
Now I am getting OutofMemoryError in more threads, rather than in just last thread.
Note: The application is multithreaded and there are 5-10 threads each collecting data for 500 objects and having their own BUfferedWriter in their call method.
public String call(){
BufferedWriter writer = new BufferedWriter(new FileWriter("filename"));
try {
List<String[]> objectList = sPoller.getobjects(Keyword.HTTP);
for(String[] objectNameArray : objectList){
if(objectNameArray.length>0){
HashMap<String, TreeMap<Long, ArrayList<String>>> statMap = new HashMap<String, TreeMap<Long, ArrayList<String>>>();
HashMap<String, String> objectMap = sPoller.getobjectNamesIDMap();
/**
*code for getting data from server
*
**/
/**
*code for populating hashmaps
*
**/
}
writeHTTPStats(statMap);
}
}catch(Exception e){
e.printStackTrace();
}finally{
writer.closeStream();
}
}