[BUG] JSONReader OOM
Nobody has claimed this yet.
- Dominant language
- Java
- Stars
- 4.4k
- Forks
- 611
- Avg merge
- 1d 22h
- Merged PRs (30d)
- 6
Description
### 问题描述
*简要描述您碰到的问题。*
JSONReader 在构造时候出现了OOM。
查看代码,内部调用了JSONReaderUTF16。该构造函数中读取了整个文件内容Arrays.copy到内存中,感觉这不是正常的流式数据访问。不清楚 如何正确使用?
### 环境信息
*请填写以下信息:*
- OS信息: [e.g.:CentOS 8.4.2105 4Core 3.10GHz 16 GB]
- JDK信息: [e.g.:Openjdk 1.8.0_312]
- 版本信息:[e.g.:Fastjson2 2.x.x]
### 重现步骤
*如何操作可以重现该问题:*
1. 使用 `xxx.xxx` 方法
2. 输入 `...` 数据
3. 出现 `...` 错误
```java
//可在此输入示例代码
```
源码中这个函数报OOM错误:
JSONReaderUTF16(Context ctx, Reader input) {
super(ctx, false, false);
this.input = input;
cacheIndex = System.identityHashCode(Thread.currentThread()) & (CACHE_ITEMS.length - 1);
final CacheItem cacheItem = CACHE_ITEMS[cacheIndex];
char[] chars = CHARS_UPDATER.getAndSet(cacheItem, null);
if (chars == null) {
chars = new char[8192];
}
int off = 0;
try {
for (; ; ) {
int n = input.read(chars, off, chars.length - off);
if (n == -1) {
break;
}
off += n;
if (off == chars.length) {
int oldCapacity = chars.length;
int newCapacity = oldCapacity + (oldCapacity >> 1);
chars = Arrays.copyOf(chars, newCapacity);
}
}
} catch (IOException ioe) {
throw new JSONException("read error", ioe);
}
### 期待的正确结果
*对您期望发生的结果进行清晰简洁的描述。*
### 相关日志输出
*请复制并粘贴任何相关的日志输出。*
Caused by: java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOf(Arrays.java:3332) ~[?:1.8.0_102]
at com.alibaba.fastjson2.JSONReaderUTF16.(JSONReaderUTF16.java:111) ~[?:?]
at com.alibaba.fastjson2.JSONReader.of(JSONReader.java:3506) ~[?:?]
at com.alibaba.fastjson.JSONReader.(JSONReader.java:22) ~[?:?]
at com.alibaba.fastjson.JSONReader.(JSONReader.java:17) ~[?:?]
at org.lxh.JsonFileSource.run(JsonFileSource.java:29) ~[?:?]
#### 附加信息
*如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。*
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Research direction
Start by reading JSONReader.of in JSONReader.java and the JSONReaderUTF16 constructor, especially the Arrays.copyOf path reported at line 111. Use the stack trace and a large Reader input to reproduce the heap failure, then determine and document an acceptable behavior for large inputs before validating parsing and memory use with focused tests.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- java
- Domain
- backend
- Issue type
- Bug
- Difficulty
- 5/5
- Estimated time
- Over a week
- Activity status
- Stale
- Clarity
- Needs clarification
- Newbie friendliness
- 25/100