Search code examples
javaheap-memorystringbuffer

Read from file - Heap full


I am trying to read from files and put them in Hash Maps. When i am reading a huge file or a file with only a sentence it works fine, But when I am reading both files it throws

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

Before putting into hashmaps i put the text into a StringBuffer. Is it the StringBuffer making the heap full? My code is :

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Scanner;

public class testforso {
public static void main(String args[]) throws FileNotFoundException {
    Scanner sc = new Scanner(System.in);
    ArrayList<String> fileNames = new ArrayList<String>();
    ArrayList<ArrayList<ArrayList<String>>> listWithFiles = new 
ArrayList<ArrayList<ArrayList<String>>>();
    String fileName;

    System.out.println("Enter the file names\t(Type stop to exit):");
    fileName = sc.next();
    while (!fileName.equals("stop")) {
        fileNames.add(fileName);
        fileName = sc.next();
    }

    for (int i = 0; i < fileNames.size(); i++) {
        Litourgia1 file = new Litourgia1(fileNames.get(i));
        listWithFiles.add(file.getWords());
    }
    System.out.println("Files read successfully");

    HashMap<String, HashMap<String, Integer>> hm = new HashMap<String, HashMap<String, Integer>>();
    HashMap<String, Integer> hm2 = new HashMap<String, Integer>();
    for (int i = 0; i < listWithFiles.size(); i++) { // for loop to get into files
        for (int j = 0; j < listWithFiles.get(i).size(); j++) { // for loop to get into sentences
            for (int k = 0; k < listWithFiles.get(i).get(j).size(); k++) { // for loop to get into words
                if (hm.containsKey(listWithFiles.get(i).get(j).get(k)))
                    hm2.put(listWithFiles.get(i).get(j).get(k), hm2.get(listWithFiles.get(i).get(j).get(k)) + 1);
                else
                    hm2.put(listWithFiles.get(i).get(j).get(k), 1);

            }
            for (int k = 0; k < listWithFiles.get(i).get(j).size(); k++) { // for loop to get into words
                if (!hm.containsKey(listWithFiles.get(i).get(j).get(k))) {
                    hm.put(listWithFiles.get(i).get(j).get(k), hm2);
                }
            }
        }
    }
    System.out.println(hm.toString()); //the Exception is in this line
}

}

Thanks


Solution

  • OutOfMemoryError is thrown when your JVM runs out of heap memory.

    If your JVM has a heap size of 512MB and you're trying to read a 1GB file, then you'd get this message because there's not enough heap memory allocated to your JVM.

    To increase it, you can use the the following arguments while running your java program:

    -Xmx2048M
    

    The following should set your maximum Java heap size to 2048MB, or 2GB.

    e.g.:

    java -jar -Xmx2048M someproject.jar
    

    If you're using an IDE, then you should be able to configure program arguments, in which case you'd just add -Xmx2048M.