alibaba / alibaba/fastjson2

[BUG] 2.0.31 序列化动态代理类,导致metaspace 内存泄漏

Open
#7,673 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
4.4k
Forks
613
Avg merge
1d 22h
Merged PRs (30d)
6

Description

### 问题描述
序列化Spring配置类,导致metaspace内存泄漏

### 环境信息
*请填写以下信息:*

- OS信息: [e.g.:MacOS 15.7.4 (24G517)]
- JDK信息: [e.g.:corretto-11.0.21]
- 版本信息:[e.g.:Fastjson2 2.0.31]

### 重现步骤
*如何操作可以重现该问题:*

1. 使用 `xxx.xxx` 方法
2. 输入 `...` 数据
3. 出现 `...` 错误
```java
// Spring配置类
@Data
@NoArgsConstructor
@Component
@RefreshScope
@ConfigurationProperties(prefix = "header.filter")
@ToString
@Slf4j
public class GlobalConfig {
private Map test1;
private Map test2;
}

// 测试类
@Component
@Slf4j
public class Test {
@Autowired
GlobalConfig config;

ThreadPoolExecutor executor = new ThreadPoolExecutor(5,10,5, TimeUnit.SECONDS, new ArrayBlockingQueue<>(10000));

@Scheduled(cron = "*/1 * * * * *")
void test() {
for (int i = 0; i < 20; i++) {
executor.submit(() -> {
log.info("test config json: {}", JSON.toJSONString(config));
});
}
}
}

```

### 期待的正确结果
内存正常

### 相关日志输出
使用Arthas查看metaspace信息
classloader-metaspace
hash classes chunkSize blockSize name
2ac05a33 10840 69328896 69197032 com.alibaba.fastjson2.util.DynamicClassLoader@2ac05a33

显示加载了几万个类

查看具体信息: sc -c 2ac05a33 *
输出:

.......
com.alibaba.fastjson2.writer.OWG_9991_2_GlobalConfig
com.alibaba.fastjson2.writer.OWG_9992_2_GlobalConfig
com.alibaba.fastjson2.writer.OWG_9993_2_GlobalConfig
com.alibaba.fastjson2.writer.OWG_9994_2_GlobalConfig
com.alibaba.fastjson2.writer.OWG_9995_2_GlobalConfig
com.alibaba.fastjson2.writer.OWG_9996_2_GlobalConfig
com.alibaba.fastjson2.writer.OWG_9997_2_GlobalConfig
com.alibaba.fastjson2.writer.OWG_9998_2_GlobalConfig
com.alibaba.fastjson2.writer.OWG_9999_2_GlobalConfig
com.alibaba.fastjson2.writer.OWG_999_2_GlobalConfig
com.alibaba.fastjson2.writer.OWG_99_2_GlobalConfig
com.alibaba.fastjson2.writer.OWG_9_2_GlobalConfig
Affect(row-cnt:15540) cost in 6074 ms.
[arthas@55854]$
[arthas@55854]$ vmtool --action forceGc

[arthas@55854]$ vmtool --action forceGc

fullgc也无法回收
[arthas@55854]$ sc -c 2ac05a33 * | wc -l
16321

#### 附加信息
本地单体应用,无数据,启动时100M,启动样例代码半小时后。到300多, 类增加到五万多个。

Image

Contributor guide

Open the contributing guide

Research direction

Start from the JSON.toJSONString(config) call in the scheduled test and use the supplied Arthas classloader and generated OWG class observations to trace repeated dynamic writer creation. Reproduce the scheduled serialization workload, then verify that generated classes and metaspace no longer grow without bound after garbage collection.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, spring
Domain
backend, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.