Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fastjson反序列化每次new ParameterizedTypeImpl导致内存泄漏问题 #3329

Closed
luliangac opened this issue Jul 6, 2020 · 4 comments
Closed
Assignees
Labels
Milestone

Comments

@luliangac
Copy link

代码中使用姿势大致如下:
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类型)内存占用越来越大,造成内存泄漏。

ZivYan added a commit to ZivYan/fastjson that referenced this issue Jul 6, 2020
@ZivYan
Copy link
Contributor

ZivYan commented Jul 6, 2020

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

@luliangac
Copy link
Author

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

@wenshao
Copy link
Member

wenshao commented Jul 8, 2020

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

@luliangac
Copy link
Author

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

@wenshao wenshao added this to the 1.2.73 milestone Jul 19, 2020
@wenshao wenshao added the bug label Jul 19, 2020
@wenshao wenshao self-assigned this Jul 19, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants