fastjson2 icon indicating copy to clipboard operation
fastjson2 copied to clipboard

[BUG] JSONReader OOM

Open metero20000 opened this issue 8 months ago • 3 comments

问题描述

简要描述您碰到的问题。

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. 出现 ... 错误
//可在此输入示例代码

源码中这个函数报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) ~[?:?]

附加信息

如果你还有其他需要提供的信息,可以在这里填写(可以提供截图、视频等)。

metero20000 avatar Jun 09 '24 02:06 metero20000