jdereg / json-io

Convert Java to JSON. Convert JSON to Java. Pretty print JSON. Java JSON serializer. Deep copy Java object graphs.
Apache License 2.0
331 stars 116 forks source link

high usage of metaspace after upgrade to 4.22.0 #283

Closed andre-pt closed 6 months ago

andre-pt commented 6 months ago

Hi,

we had an application for some years working with 4.14.0 and today we rolled out an update to 4.22.0.

We noticed instances getting java.lang.OutOfMemoryError: Metaspace and started investigating.

Metaspace usage was constant before this update and now we had to increase it to avoid running into OutOfMemory: image

We also noticed that loaded classes increased significantly: image

We are using the following 2 methods:

    public static String toJson(Object o) {
        return JsonIo.toJson(o, null);
    }

    public static <T> T toObject(String json, Class<T> rootType) {
        return JsonIo.toObjects(json, null, rootType);
    }

Is this behavior expected? Is there anything we can do to limit the usage of metaspace?

Thanks a lot Andre

andre-pt commented 6 months ago

I just noticed a huge performance improvement by reusing the ReadOptions and WriteOptions:

    private static final WriteOptions DEFAULT_WRITE_OPTIONS = new WriteOptionsBuilder().build();
    private static final ReadOptions DEFAULT_READ_OPTIONS = new ReadOptionsBuilder().build();

    public static String toJson(Object o) {
        return JsonIo.toJson(o, DEFAULT_WRITE_OPTIONS);
    }

    public static <T> T toObject(String json, Class<T> rootType) {
        return JsonIo.toObjects(json, DEFAULT_READ_OPTIONS, rootType);
    }

We will try this on production and I will report back

andre-pt commented 6 months ago

my previous solution resolved the metaspace usage issue.

jdereg commented 6 months ago

Andre, thank you for posting your ReadOptions/WriteOptions journey. You are absolutely correct in your conclusion, a ReadOptions / WriteOptions instance should be created and held onto. I will make sure our documentation is more clear on that. Thank you again for your posts.

andre-pt commented 6 months ago

Hey As an alternative, you could make a singleton or a static default object used when the parameter is null It would avoid issues for less careful users like me.

Additionally we are experiencing a memory leak in LRUCache class. I will investigate more next week, but it's very similar to what's described here: https://hoangx281283.wordpress.com/2012/11/18/wrong-use-of-linkedhashmap-causes-memory-leak/

Andre

On Fri, May 31, 2024, 16:59 John DeRegnaucourt @.***> wrote:

Andre, thank you for posting the journey you made. You are absolutely correct in your conclusion, that the intention is that we expect a ReadOptions instance and a WriteOptions instance should be created and held onto. I will make sure our documentation is more clear on that. Thank you again for your posts.

— Reply to this email directly, view it on GitHub https://github.com/jdereg/json-io/issues/283#issuecomment-2142450231, or unsubscribe https://github.com/notifications/unsubscribe-auth/AB77EKYPQBYGIXDFE5OFTALZFCF3TAVCNFSM6AAAAABINAZLXWVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCNBSGQ2TAMRTGE . You are receiving this because you modified the open/close state.Message ID: @.***>