简体   繁体   中英

OutOfMemoryError when converting objects to json using GSON

I am using the GSON Stream API to convert a large list of objects to json. The list contains 110.000 objects. I use this code:

public static void cacheToFile() {

    Gson gson = new GsonBuilder()
                .setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES)
                .registerTypeAdapter(Date.class, new DateTypeAdapter())
                .create(); 

    try {

        JsonWriter writer = new JsonWriter(new FileWriter(Functions.getAppSupportPath()+"cache.json"));
        writer.beginArray();

        int i = 1;
        synchronized(cache) {

            for (CachedObject<?> object : cache) {
                gson.toJson(object, CachedObject.class, writer);
                System.out.println(i);
                i++;
            }

        }

        writer.endArray();
        writer.close();

    }
    catch (Exception e) {
        Functions.createExceptionDialog(e);
    }

}

The loop reaches 55.000 and then freezes for some time. After that it throws the following error:

java.lang.OutOfMemoryError: GC overhead limit exceeded
at com.google.gson.internal.$Gson$Types$WildcardTypeImpl.getUpperBounds($Gson$Types.java:556)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:373)
at com.google.gson.internal.$Gson$Types.resolve($Gson$Types.java:376)
...

I thought the Stream API would prevent memory issues, but in this case it doesn't. How can I convert the list of objects to json, preventing this issue?

Any help is greatly appreciated!

The reason that your looop freezes and then crashes is that the GC is trying to free enough memory to continue the computation, but it's unable to do it. This is probably being caused by keeping too many elements in memory at the same time. The only way you can solve it is by increasing the memory allocated for the JVM or by refactoring the code to reduce the memory complexity.

It could be helpfull if you could include the full stack trace.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM