gpt4 book ai didi

java - 读取大文件时OutofMemoryError

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:30:58 25 4
gpt4 key购买 nike

我正在尝试读取几个大文件(超过 100MB)。到目前为止,它总是在中间崩溃并出现 OutofMemory 错误。有什么解决办法吗?

           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);

最佳答案

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

关于java - 读取大文件时OutofMemoryError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7996801/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com