加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 读取大文件时的OutofMemoryError

发布时间:2020-12-15 05:07:16 所属栏目:Java 来源:网络整理
导读:我正在尝试阅读几个大文件(超过100MB).到目前为止,它总是在OutofMemory Error中间打破.有什么解决方案吗? FileInputStream fstream = new FileInputStream(f); // Get the object of DataInputStream DataInputStream dain = new DataInputStream(fstream);
我正在尝试阅读几个大文件(超过100MB).到目前为止,它总是在OutofMemory Error中间打破.有什么解决方案吗?

FileInputStream fstream = new FileInputStream(f);
          // Get the object of DataInputStream
        DataInputStream dain = new DataInputStream(fstream);
        //  BufferedReader br = new BufferedReader(new InputStreamReader(in));

        BufferedReader in = new BufferedReader(new InputStreamReader(dain));
        String text = in.readLine();
        while(text != null) {
            stat(text);
            text = in.readLine();
        }

例外是这样的:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at java.util.Arrays.copyOfRange(Arrays.java:2694)
at java.lang.String.<init>(String.java:234)
at java.io.BufferedReader.readLine(BufferedReader.java:349)
at java.io.BufferedReader.readLine(BufferedReader.java:382)

以下是Stat的作用:

public void stat(String text) {
    String postTypeId = this.getXmlValue(text,"PostTypeId");
    String viewCountStr = this.getXmlValue(text,"ViewCount");
    String answerCountStr = this.getXmlValue(text,"AnswerCount");
    String userId = this.getXmlValue(text,"OwnerUserId");
    String postId = this.getXmlValue(text,"Id");
    String parentId = this.getXmlValue(text,"ParentId");
    String backUpId = this.getXmlValue(text,"LastEditorUserId");
    //Add post rel
    if(parentId==null) {
        if(!postTable.containsKey(postId)) 
            postTable.put(postId,new PostRel());
    } else {
        try{
        postTable.get(parentId).addAnswer(postId);
        }catch(Exception exp) {
        }
    }
              generalCount(postTypeId,viewCountStr,answerCountStr,userId,postId,parentId,backUpId);

}

在generalCount中,我试图插入另一个表:

if(userTable.containsKey(userId)) {
        userTable.get(userId).addPost(postId);
        if(parentId!=null)
            userTable.get(userId).addAnswer(parentId);
    } else{
        UserPostInfo newInfo = new UserPostInfo();
        newInfo.addPost(postId);
        if(parentId!=null)
            newInfo.addAnswer(parentId);
        userTable.put(userId,newInfo);

解决方法

> Give the JVM more memory to work with
>在读取文件时使用更少的内存(您可以使用流数据吗?)
>使用 memory-mapped files

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读