How to avoid OutOfMemory exception while reading large files in Java

徘徊边缘 提交于 2019-12-01 12:58:51

When reading big files, parsing objects and keeping them in memory there are several solutions with several tradeoffs:

  1. You can fit all parsed objects into memory for that app deployed on one server. It either requires to store all objects in very zipped way, for example using byte or integer to store 2 numbers or some kind of shifting in other data structures. In other words fitting all objects in possible minimum space. Or increase memory for that server(scale vertically)

    a) However reading the files can take too much memory, so you have to read them in chunks. For example this is what I was doing with json files:

    JsonReader reader = new JsonReader(new InputStreamReader(in, "UTF-8"));
        if (reader.hasNext()) {
            reader.beginObject();
            String name = reader.nextName();
    
            if ("content".equals(name)) {
                reader.beginArray();
    
                parseContentJsonArray(reader, name2ContentMap);
    
                reader.endArray();
            }
            name = reader.nextName();
            if ("ad".equals(name)) {
                reader.beginArray();
    
                parsePrerollJsonArray(reader, prerollMap);
    
                reader.endArray();
            }
        }
    

    The idea is to have a way to identify when certain object starts and ends and read only that part.

    b) You can also split files to smaller ones at the source if you can, then it will be easier to read them.

  2. You can't fit all parsed objects for that app on one server. In this case you have to shard based on some object property. For example split data based on US state into multiple servers.

Hopefully it helps in your solution.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!