[alibaba/fastjson]fastjson反序列化每次new ParameterizedTypeImpl导致内存泄漏问题

2024-05-22 417 views
0

代码中使用姿势大致如下: ParameterizedTypeImpl type = new ParameterizedTYpeImpl(new Type[]{ SomeInfo.class }, null, CommonVO.class); CommonVO result = (CommonVO)JSON.parseObject(jsonString, type);

原因: com.alibaba.fastjson.ParserConfig定义一个字段用于缓存不同类的反序列化器,使用的是IdentityHashMap(IdentityHashMap使用的是==比较key的值,不同于HashMap使用equals比较),缓存是以Type为key: private final IdentityHashMap<Type, ObjectDeserializer> deserializers = new IdentityHashMap<Type, ObjectDeserializer>();

随着系统的运行,ParserConfig的deserializers(IdentityHashMap类型)内存占用越来越大,造成内存泄漏。

回答

8

IdentityHashMap 里面的 buckets size 是固定的,所以不会越来越大

9

buckets size 是固定的,但是链式map可以继续增加对象数量

3

使用ParameterizedTypeImpl,需要单例使用,这种方式是有问题的

8

软件开放出来的东西如果用起来有问题就是bug,否则可以把这个类设置成protect