简体   繁体   English

内存不足LinkedList添加错误

[英]OutofMemory LinkedList Adding Error

I am trying to read from a txt file (book) and then add every line of it to a linkedlist. 我正在尝试从txt文件(书)中读取内容,然后将其每一行添加到链表中。 However, when I run the code, I got an outofmemory error at l.add(line); 但是,当我运行代码时,在l.add(line);处出现l.add(line);不足错误l.add(line); . Could you tell me what I am doing wrong with this code? 您能告诉我这段代码在做什么吗? Or, is there a better way to store the String values instead of LinkedList? 或者,有没有更好的方法来存储String值而不是LinkedList?

Thanks a lot! 非常感谢!

public Book (String bookname) throws java.io.IOException{
    f = new FileReader(bookname);
    b = new BufferedReader(f);
    l = new LinkedList<String>();
    String line = b.readLine();
    while (line != null) {
        l.add(line);
    }
    b.close();
}

As others point out, you have created an infinite, memory-consuming loop. 正如其他人指出的那样,您已经创建了一个无限的,消耗内存的循环。 A common idiom for reading from a BufferedReader is: 从BufferedReader读取的常见用法是:

String line;
while ( ( line = b.readLine() ) != null) {
    l.add(line);
}

I guess it is possible that the content of the book is just too large to fit into memory all at once anyway. 我想这本书的内容可能太大而无法一次全部放入内存。 You can increase the memory available to the JVM by using the Xmx argument, ie: 您可以使用Xmx参数来增加JVM可用的内存,即:

java -Xmx1G MyClass

The default value for this is 64 Mb, which isn't much these days. 默认值是64 Mb,这些天来已经不多了。

You are adding the same line over and over, until memory runs out: 您要一遍又一遍地添加同一行,直到内存用完:

String line = b.readLine();
while (line != null) {
    l.add(line);
}

See? 看到? The line -variable is read outside the loop, and never changes within the loop. -variable 行在循环外部读取,在循环内永不更改。

Probably you should replace 可能您应该更换

while (line != null) {
    l.add(line);
}

with

while (line = b.readLine()) {
    l.add(line);
}

While loop never quits, because variable line is never null. while循环永远不会退出,因为可变line永远不会为空。 Try this: 尝试这个:

String line = "";
while ((line = b.readLine())!= null)
{
   l.add(line);
}
b.close();

Quite simply, the memory required to store the strings (and everything else in your program) exceeded the total free memory available on the heap. 很简单,存储字符串(以及程序中的所有其他内容)所需的内存超过了堆上可用的总可用内存。

Other lists will have slightly different amounts of overhead, but realistically the memory requirements of the structure itself is likely to be insignificant compared to the data it contains. 其他列表的开销将略有不同,但是实际上,结构本身的内存要求与其所包含的数据相比可能微不足道。 In other words, switching to a different list implementation might let you read a few more lines before falling over, but it won't solve the problem. 换句话说,切换到其他列表实现可能会让您在翻倒前多读几行,但不能解决问题。

If you haven't increased the heap space of the java application, it might be running with fairly low defaults. 如果您没有增加Java应用程序的堆空间,那么它可能会以较低的默认值运行。 In which case, you should consider providing the following command-line argument to your invocation of java : 在这种情况下,您应该考虑在调用java提供以下命令行参数:

-Xmx512m

(where the 512m implies 512 megabytes of heap space; you could use eg -Xmx2g or whatever else you feel is appropriate.) 512m意味着512 MB的堆空间;您可以使用-Xmx2g或其他您认为合适的方法。)

On the other hand, if you're already running with a large heap (much larger than the total size of the Strings you want to hold in memory), this could point to a memory problem somewhere else. 另一方面,如果您已经运行了一个大堆(比要保留在内存中的String的总大小大得多),这可能表明其他地方存在内存问题。 How many characters are there in the book? 书中有几个字符? It will take at least twice that many bytes to store all of the lines, and probably 20% or so more than that to account for overhead. 存储所有行至少需要两倍的字节数,并且可能要多出20%左右才能解决开销。 If your calculations indicate that your current heap space should be able to hold all of this data, there might be some trouble elsewhere. 如果您的计算表明您当前的堆空间应该能够容纳所有这些数据,那么其他地方可能会遇到麻烦。 Otherwise, you now know what you'd need to increase you heap to as a minimum. 否则,您现在知道将堆增加到最低限度需要什么。

(As an aside, trying to process large amounts of input as a single batch can often run into memory issues - what if you want to process an 8GB text file? Often it's better to process smaller chunks sequentially, in some sort of stream. For example, if you wanted to uppercase every character and write it back out to a different file, you could do this a line at a time rather than reading the whole book into memory first.) (顺便说一句,尝试作为一个批处理大量输入通常会遇到内存问题-如果要处理8GB文本文件该怎么办?通常最好以某种流顺序处理较小的块。对于例如,如果您要大写每个字符并将其写回到另一个文件中,则可以一次执行一行操作,而不是先将整本书读入内存中。)

I agree with mjg123. 我同意mjg123。 And be careful with the outofmemory expection. 并注意内存不足的期望。 and i request you to have to look at this blog for more details of how to handle such situations Click here 我要求您必须查看此博客以获取有关如何处理此类情况的更多详细信息,请单击此处。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM