0

When a large set of JSON object is cast to Java Object using ObjectMapper throws java.lang.OutOfMemoryError: GC overhead limit exceeded. Find the code snippets here

(List<ClassA>) JSONUtils.toObjectList(responseEntity.getBody(), new TypeReference<List<ClassA>>() {})
public class JSONUtils  {
    ... 
    private static final ObjectMapper jsonToObjectListMapper = new ObjectMapper();  
    ...
    public static Object toObjectList(String json, TypeReference<?> type) {
        Object obj = null;
        try {
            obj = jsonToObjectListMapper.readValue(json, type);
        }
        catch (Exception e) {
            throw e;
        }
        return obj;
    }
    ...
}

Is there any other solution that means any library which can efficiently parse JSON?

Thilakraj
  • 63
  • 1
  • 10
  • 2
    You can use [Jackson streaming](https://github.com/FasterXML/jackson-docs/wiki/JacksonStreamingApi) – aksappy Aug 13 '21 at 16:31
  • or [javax.json.JSONParser](https://stackoverflow.com/questions/25064773/working-with-json-streams-efficiently-in-java) – eis Aug 13 '21 at 16:34
  • When does it fail? Does it fail in `readValue` or when you construct a string out of the JSON? As @aksappy suggested, use streams and not strings that also might allocate much heap space (if you run out of heap space, then your approach is definitely wrong in at least one place). Aside of that, a matter of code style, but your 8 lines of code might/should be simplified to `return jsonToObjectListMapper.readValue(json, type);` (no jagged `obj` declaration/assignment/returning, no useless try/catch.) – terrorrussia-keeps-killing Aug 14 '21 at 06:04
  • @fluffy Yes. the exception is thrown while ```readValue```gets executed. Agreed about code style. In this line, we are performing other operations and throwing custom exceptions while pasting here I omitted those lines – Thilakraj Aug 14 '21 at 16:14

0 Answers0