【问题标题】:How to fix an Out Of memory when importing a large JSON file导入大型 JSON 文件时如何修复内存不足
【发布时间】:2020-08-18 17:49:25
【问题描述】:

我想导入一个大小为 5 GB 的 JSON 文件,但它显示了一个 Out of memory 错误。 我通过输入-Xmx7700m -Xms7700m -XX: + UseConcMarkSweepGC 设置 JVM,知道我的计算机中有 8 GB 的 RAM,但程序执行需要 45 分钟,然后显示此错误: 我正在使用 maven depedency "com.googlecode.json-simple" 版本:1.1.1

线程“主”java.lang.OutOfMemoryError 中的异常:GC 开销 超出限制 java.lang.OutOfMemoryError: Java heap space //如果我放 -Xmx5000m -Xms5000m

这是导入 JSON 文件的代码

 JSONParser parser = new JSONParser();

    try {
        Object obj = parser.parse(new FileReader("url.json"));

        JSONObject jsonObject =  (JSONObject) obj;
        System.out.println(jsonObject);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ParseException e) {
        e.printStackTrace();
    }

我能找到另一种解决方案来将 JSON 文件分成几部分并逐部分导入吗?

【问题讨论】:

  • 您不认为您使用的 what JSON 解析器很重要吗?!换句话说:请告诉我们您正在使用哪个库/工具,以及使用的是哪个版本。
  • 尝试将 5GB 文件加载到内存中是一个糟糕的设计。您应该查看支持流 api 的解析器。
  • 除此之外,您应该查看您的要求。您真的想要所有这些 5 GB 的 JSON 同时存储在内存中,还是解析/处理/转换...“较小”垃圾中的数据会更好?记录一下:一个 5 GB 的 JSON 文件……听起来很奇怪。
  • 如果你有 8G 你不能使用 7.7G 作为堆。很可能计算机正在交换。另外,无论如何解析后,您可能需要超过 7G 才能将 5G 文件保留在内存中。您需要使用流式解决方案,否则您将需要获得更多 RAM!
  • @GhostCat 我把它放到了问题中

标签: java json


【解决方案1】:

假设您的 JSON 文件是一个大型 JSON 对象数组,这对您有帮助吗?:

import com.google.gson.stream.JsonReader;
import org.json.simple.JSONObject;
...


  JsonReader jsonReader = new JsonReader(new InputStreamReader(new FileInputStream(theFile), StandardCharsets.UTF_8));
  jsonReader.beginArray();
  Gson gson = new GsonBuilder().create();
  while (jsonReader.hasNext()) {
       JSONObject currentJsonObject = gson.fromJson(jsonReader, JSONObject.class);
        // do stuff
   }
   jsonReader.close();

【讨论】:

    【解决方案2】:
    // Create a mapper model class according with your JSON file fields. I gave an example.   
    public class JsonToJavaObject {
    
        @SerializedName("Id")
        @Expose
        private Integer id;
    
        @SerializedName("Name")
        @Expose
        private String name; 
    
       // Getter/Setter
    }
    
    // Create file from your JSON
    File file = new File("url.json"); // Make sure your file name and location
    
    // Make an input stream for the file's data
    BufferedInputStream buf = new BufferedInputStream(new FileInputStream(file));
    InputStreamReader inputStreamReader = new InputStreamReader(buf, StandardCharsets.UTF_8);
    
    // Read stream of data
    try (JsonReader jsonReader = new JsonReader(inputStreamReader)) {
         Gson gson = new GsonBuilder().create();
         // Create JSON object
         JsonToJavaObject = gson.fromJson(jsonReader, JsonToJavaOnline.class);
    } catch (Exception e) {
       e.getMessage();
    }
    

    【讨论】:

      猜你喜欢
      • 2023-04-09
      • 2019-02-19
      • 1970-01-01
      • 2012-06-11
      • 1970-01-01
      • 2012-03-19
      • 2011-08-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多