0

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();
    }
}
Dhanesh Khurana
  • 159
  • 1
  • 13
  • This question has been asked already. Please find some info in http://stackoverflow.com/questions/12202313/my-java-program-which-reads-a-large-text-file-is-running-out-of-memory-can-anyo – Vargan Aug 26 '15 at 09:32
  • What is the amount of heap allocated to your application? What would be the aprrox. size of all the objects retrieved from server? – Loganathan Mohanraj Aug 26 '15 at 09:33
  • @LoganathanMohanraj : memory allocated is 1gb, and object just consists of 4 to 10 string objects (some property values) and 1 calendar object (containg date-timestamp). – Dhanesh Khurana Aug 26 '15 at 10:26

0 Answers0