0

I have to read two seperate txt files into memory. I will read each line from one txt file and will look for some contents from one txt file in the the other txt file several times.

I tried to read the txt files into two seperate ArrayLists but when I tried to check the size after loading, it threwjava.lang.OutOfMemoryError: Java heap space exception.

This is my code block:

BufferedReader mtc_txt = new BufferedReader(new FileReader(mtc_path));
BufferedReader mfc_txt = new BufferedReader(new FileReader(mfc_path));

String line;

ArrayList<String> mtc_txt_list = new ArrayList<String>();
ArrayList<String> mfc_txt_list = new ArrayList<String>();

System.out.println("Loading..");
while ((line = mtc_txt.readLine()) != null) {

mtc_txt_list.add(line);

}
while ((line = mfc_txt.readLine()) != null) {
mfc_txt_list.add(line);
}
System.out.println("Loaded..");
System.out.println("MTC num of lines: "+mtc_txt_list.size());
System.out.println("MFC num of lines: "+mfc_txt_list.size());

This is the output:

Loading..
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOfRange(Unknown Source)
at java.lang.String.<init>(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at java.io.BufferedReader.readLine(Unknown Source)
at Main.replaceSelected(Main.java:39)
at Main.main(Main.java:108)

one txt file contains about 90000 lines, and the other contains 524000 lines.

I want to keep them in the memory. What is the best way to do this in Java?

Thanks.

Ugur KAYA
  • 167
  • 3
  • 14
  • 2
    Possible duplicate of [best way of loading a large text file in java](http://stackoverflow.com/questions/26156360/best-way-of-loading-a-large-text-file-in-java) – David Jul 12 '16 at 10:55
  • If you **really** need to keep them in memory: increase your maximum heap-size. – piet.t Jul 12 '16 at 10:58
  • To be honest, I don't have much experience with file io operations. So I don't know if I really need to keep them in memory. I need to look for each line data from one txt in the other txt. so I thought that keeping them in the memory would be faster. – Ugur KAYA Jul 12 '16 at 11:00
  • Well, that just means keeping the one you search in in memory. The other you can just read line by line and discard. – RealSkeptic Jul 12 '16 at 11:02
  • @RealSkeptic seems logical. I'll give it a try. by the way my heap size is passed as -Xmx1024m to the VM. – Ugur KAYA Jul 12 '16 at 11:08

0 Answers0