0
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).
calms(verb,[sg],calm).
calmed(verb,[sg,pl,en],calm).
calm(verb,[pl,inf],calm).
calming(verb,[ing],calm).
calmly(adv,mv,calmly).
calmness(noun,[sg],calmness).

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. How can i fix this memory problem?

trincot
  • 317,000
  • 35
  • 244
  • 286
dirdir69
  • 47
  • 7
  • Try this with a small file to test. – Anubian Noob May 16 '14 at 17:35
  • Check out this post: http://stackoverflow.com/questions/9619092/java-heap-space-hashmap-arraylist?rq=1 – chickenpie May 16 '14 at 17:37
  • It's a huge file with several thousand entries. All right, I am going to trim the file and check it out. – dirdir69 May 16 '14 at 17:42
  • Yeah, it works fine for a smaller file. Is there a way I could make it more efficient so that less heap space would be used? Or can i increase the heap memory I am using? I am running this with NetBeans 8 and Ubuntu – dirdir69 May 16 '14 at 18:08

2 Answers2

0

One suggestion is that instead of doing 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());
   }

}
Mark Carpenter
  • 433
  • 3
  • 9
0

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. 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.

Arne Burmeister
  • 20,046
  • 8
  • 53
  • 94