I am trying to parse large JSON file with JSON Simple and i am getting out of memory errors. I am on Windows 10 and my laptop has an 8gb RAM. The file is 250mb, i will also need to parse a 2gb file. I also tried with StrinBuilder but then i am getting memory errors on StringBuilder. Here is my code with StringBuilder:
BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream("myfile.json")));
String line = null;
StringBuilder sb= new StringBuilder("");
while( (line = br.readLine())!= null ){
sb.append(line);
}
JSONParser parser = new JSONParser();
Object obj=null;
try {
obj = parser.parse(sb.toString());
}catch (Exception e) {
}
and here is the code without StringBuilder:
JSONParser parser = new JSONParser();
Object obj=null;
try {
obj = parser.parse(new FileReader("myfile.json"));
}catch (Exception e) {
}
The error
Exception in thread "main" java.lang.OutOfMemoryError: GC overhead limit exceeded at org.json.simple.parser.Yylex.yylex(Unknown Source) at org.json.simple.parser.JSONParser.nextToken(Unknown Source) at org.json.simple.parser.JSONParser.parse(Unknown Source) at org.json.simple.parser.JSONParser.parse(Unknown Source)
StringBuilderobject holds all the data during iteration. so that all contents in the file is in memory. you can follow the link that show's how to read a big size file. stackoverflow.com/questions/2356137/read-large-files-in-java