OutofMemory 链表添加错误

发布于 2024-10-20 04:27:39 字数 444 浏览 2 评论 0原文

我正在尝试读取 txt 文件(书),然后将其每一行添加到链接列表中。但是,当我运行代码时,我在 l.add(line); 处收到内存不足错误。你能告诉我这段代码做错了什么吗?或者,是否有更好的方法来存储字符串值而不是 LinkedList?

多谢!

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

I am trying to read from a txt file (book) and then add every line of it to a linkedlist. However, when I run the code, I got an outofmemory error at 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?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

二手情话 2024-10-27 04:27:39

正如其他人指出的那样,您创建了一个无限的、消耗内存的循环。从 BufferedReader 中读取内容的一个常见习惯用法是:

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

我猜这本书的内容可能太大,无法一次全部装入内存。您可以使用 Xmx 参数来增加 JVM 可用的内存,即:

java -Xmx1G MyClass

默认值为 64 Mb,目前这个值已经不多了。

As others point out, you have created an infinite, memory-consuming loop. A common idiom for reading from a BufferedReader is:

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:

java -Xmx1G MyClass

The default value for this is 64 Mb, which isn't much these days.

卷耳 2024-10-27 04:27:39

您一遍又一遍地添加同一行,直到内存耗尽:

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

看到了吗? line-变量在循环外部读取,并且在循环内永远不会改变。

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.

假面具 2024-10-27 04:27:39

也许你应该替换

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

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

Probably you should replace

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

with

while (line = b.readLine()) {
    l.add(line);
}
桃扇骨 2024-10-27 04:27:39

while 循环永远不会退出,因为变量
line 永远不会为空。试试这个:

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

While loop never quits, because variable
line is never null. Try this:

String line = "";
while ((line = b.readLine())!= null)
{
   l.add(line);
}
b.close();
寄居人 2024-10-27 04:27:39

很简单,存储字符串(以及程序中的其他所有内容)所需的内存超过了堆上可用的总可用内存。

其他列表的开销量略有不同,但实际上结构本身的内存需求与其包含的数据相比可能微不足道。换句话说,切换到不同的列表实现可能会让您在失败之前多读几行,但这并不能解决问题。

如果您没有增加 java 应用程序的堆空间,它可能会以相当低的默认值运行。在这种情况下,您应该考虑为 java 的调用提供以下命令行参数:(

-Xmx512m

其中 512m 意味着 512 MB 的堆空间;您可以使用例如 < code>-Xmx2g 或您认为合适的任何其他内容。)

另一方面,如果您已经在运行一个大堆(比您想要在内存中保存的字符串的总大小大得多),这可能表明其他地方存在内存问题。书中有多少个人物?存储所有行至少需要两倍的字节数,并且可能比这个多 20% 左右以考虑开销。如果您的计算表明您当前的堆空间应该能够容纳所有这些数据,那么其他地方可能会出现一些问题。否则,您现在知道需要什么才能将堆增加到最小值。

(顺便说一句,尝试将大量输入作为单个批次处理通常会遇到内存问题 - 如果您想处理 8GB 文本文件怎么办?通常最好在某种流中顺序处理较小的块。对于例如,如果您想将每个字符大写并将其写回到不同的文件中,您可以一次一行执行此操作,而不是先将整本书读入内存。)

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. In which case, you should consider providing the following command-line argument to your invocation of java:

-Xmx512m

(where the 512m implies 512 megabytes of heap space; you could use e.g. -Xmx2g or whatever else you feel is appropriate.)

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

甜扑 2024-10-27 04:27:39

我同意 mjg123 的观点。并小心内存不足的预期。我请求您查看此博客以获取有关如何处理此类情况的更多详细信息
点击这里

I agree with 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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文