0

So i have a huge JSONObject that I need to write to a file, right now my code work perfectly on 90% of the devices, the problem is on low memory devices such as Amazon Fire TV the app crashes with an error "java.lang.OutOfMemoryError". I wonder is there another more memory friendly way to write that json object to file?

That's my code:

    try{
        Writer output = null;
        if(jsonFile.isDirectory()){
            jsonFile.delete();
        }
        if(!jsonFile.exists()){
            jsonFile.createNewFile();
        }
        output = new BufferedWriter(new FileWriter(jsonFile));
        output.write(mainObject.toString());
        output.close();

    } catch (Exception e) {
        e.printStackTrace();
    }
4
  • 1
    Use a streaming JSON option, such as JsonWriter. Commented Jul 16, 2019 at 16:40
  • for avoiding OOM, using largeHeap is an option, but not recommended though. Commented Jul 16, 2019 at 16:41
  • Already using largeHeap... @DarShan Commented Jul 16, 2019 at 16:46
  • Check this. stackoverflow.com/questions/40168601/… Commented Jul 16, 2019 at 17:14

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.