gpt4 book ai didi

java - 堆重载JVM

转载 作者:行者123 更新时间:2023-12-02 04:45:50 27 4
gpt4 key购买 nike

编辑***:就目前而言,我有一个程序以某种方式陷入无限循环,但是我不确定是什么原因引起的。我的程序从文本文件中读取数据,并从本质上“锯掉”每一行的第一部分数据,直到首次出现','字符,然后读取每个后续​​字符串(通常情况下以''终止) ),直到下一个','(特殊情况,表示后面的数据也是垃圾)。从那里,它跳到下一行并重复。几周前,我的程序运行良好,但我对此进行了修改,现在它正式被破坏了。在此编辑下方是我遇到的其他一些错误(可能仍需要解决),但此刻,我在某个地方被此循环杀死。我承认我有一个诀窍,就是用嵌套的FOR循环搞砸自己,并使用时髦的迭代器和更新,但是我只是无法破解。这是代码,并带有一些解释。整个代码段仅被放入带有字符串“ synsets”的某个任意类的构造函数中,并在我的main中创建了一个实例。我已评论的其余内容尽可能最佳:

    In in = new In(synsets); // Custom input stream class, courtesy 
// of Princeton U
Out fout = new Out("log.txt"); // ostream analogue

int linecount=0; // Marker used to keep track of line # in input file
int nouncount=0; // Marker for keeping track of the number of
// "important" data items

// Data comes in the format:
// "junk,important important important,junk
// junk,important,junk
// junk,important important,junk" etc.

{
int i=0;
for ( String str=in.readLine();str!=null;str=in.readLine() )
{
i=0; // reset iterator for a new line
for ( char next=str.charAt(i); next!=',';next=str.charAt(i) )
i++; // This FOR loop cuts out the junk at the start of
// a line
i++; // increment to after first comma
for ( char next=str.charAt(i);next!=',';next=str.charAt(i))
{
for (; next!=',' && next!=' ' ;next=str.charAt(i))
{
i++;
fout.print(next);
// count the "nouns" (important data) on a line
}
nouncount++;
// count the last noun on line, and subsequently fall
// through loops to skip the rest of the junk at the
// current line
fout.print('\n');
}

}
fout.print(nouncount);
in.close();


我的输出文件很好,直到输入的第18行,这时它才开始打印新的行字符(例如它被捕获在将它们附加到循环中但不能落入下一个循环的循环中)。这是有问题的输入线。它读取24-karat_gold很好,但由于某种原因未将其读取为pure_gold。有什么想法吗?谢谢你们!

“ 17,24克拉纯金100%黄金
18,24 / 7,正常运行时间是每周7天每天24小时”

编辑*:我正在编码一个程序,以从文本文件中读取一些数据,并收到一条新的(对我来说)错误消息,提示我堆空间不足。我尝试修补该错误,但仅成功消除了错误消息。我正在读取的文本文件大约有9万行,但是我的程序在第18行抛出了消息。我的修改确实揭示了一些东西,特别是,我的程序至少能够对整个文件进行某些处理,并且该问题可能涉及某些数据损坏。我之所以得出这个结论,是因为我使用的数组索引是“ j”,它触发了IndexOutOfBoundsException。问题j每次增加j时仅按1缩放,以某种方式,它超出了比数组范围高150,000的范围。因此,我假设“ j”以某种方式填充了数据(可能是因为堆栈正在覆盖到堆中?)与“ j”或所讨论的数组无关。以下是我的原始文章,尽管我确实很累,所以这可能没有多大意义。谢谢你! :)

编辑**:我检查了名词计数,它是399850,所以我没有像我所怀疑的那样遭受数据损坏。我的循环之一就是无限执行广告。如果可以解决问题,我将尝试找出哪一个并发回。至少以后我会再为所有有需要的人整理一下。

我正在编写一个程序,该程序读取一个文本文件,并且必须进行一些计数(文件中的数据以非常语法的方式进行划分),但是我的代码无法做到那么远。虽然文件相当大(大约90K行),但我的代码在堆溢出之前只经过17次。

在某些背景下,“ In”类只是一个专门的输入流,实际上杀死我的不是第一个FOR循环,我在其中计算行数(代码很好地执行了这一部分,逐行读取每行一次)。而是第二部分,但是我不明白,因为在那部分中我没有使用任何额外的堆空间(我认为吗?)。我以前把这些块放在一起,逐行地一起执行主要功能,但是程序再次在第17行停止。我尝试在Java博士中分配最多GB的堆空间,但无济于事。下半部分是否有任何部分可以预留额外的堆空间?

        In in = new In(synsets);

StringBuilder nounData = new StringBuilder();
int linecount=0;
int nouncount=0;

{
String str;
int i=0;
char next='\0';
for ( str=in.readLine();str!=null;str=in.readLine() )
{
linecount++;
}
in.close();

in = new In(synsets);
for ( str=in.readLine();str!=null;str=in.readLine() )
{
i=0;
// The first portion of each line is "trash" until the first comma
for ( next=str.charAt(i) ; next!=',' ; next=str.charAt(++i) ){}
i++;

// This actually reads/processes the data until the next comma, then
// jumps to the next line. "What" i need done is really secondary, I
// just need to figure out what is eating so much space so I can
// trim it
for ( next=str.charAt(i);next!=',';next=str.charAt(i))
for (;next!=','&&next!=' ';next=str.charAt(++i))
nouncount++;
}
}


这是更新的代码,其中我尝试从Java博士重新创建Heap消息。虽然我不能做到这一点,但确实收到了关于数组超出范围的有趣的错误消息(当然,在第18行)。但是仍然很困惑,因为我看不到有问题的变量在被命中时怎么可能超过17。

这是代码:

In in = new In(synsets);

StringBuilder nounData = new StringBuilder();
int linecount=0;
int nouncount=0;

{
int i=0;
for ( String str=in.readLine();str!=null;str=in.readLine() )
{
i=0;
for ( char next=str.charAt(i);next!=',';next=str.charAt(i))
for (;next!=','&&next!=' ';next=str.charAt(++i))
nouncount++;
}
in.close();
in=new In(synsets);

String[] nouns = new String[nouncount];
int j=0;
for ( String str=in.readLine();str!=null;str=in.readLine() )
{
linecount++;
i=0;
for ( char next=str.charAt(i) ; next!=',' ; next=str.charAt(++i) ){}
i++;
for ( char next=str.charAt(i);next!=',';next=str.charAt(i))
{
for (;next!=','&&next!=' ';next=str.charAt(++i))
nounData.append(next);
nouns[j++]=nounData.toString();
nounData.delete(0,nounData.capacity()-1);
}
System.out.println("Current line count is: " + linecount);
}
}
in.close();
System.out.println("line count = "+linecount);
System.out.println("noun count = "+nouncount);
String[] nouns = new String[nouncount];


这是错误消息:

Current line count is: 1
Current line count is: 2
Current line count is: 3
Current line count is: 4
Current line count is: 5
Current line count is: 6
Current line count is: 7
Current line count is: 8
Current line count is: 9
Current line count is: 10
Current line count is: 11
Current line count is: 12
Current line count is: 13
Current line count is: 14
Current line count is: 15
Current line count is: 16
Current line count is: 17
java.lang.ArrayIndexOutOfBoundsException: 399850
at WordNet.<init>(WordNet.java:39)
at WordNet.main(WordNet.java:212)


作为记录,我的代码中的“ 39”行是:

nouns[j++]=nounData.toString();


直到第18行的文件中的字符数为917,在第19行的字符数为966,所以我认为我放错了该行。

编辑:此外,我做了一个测试,文件中只有大约147K的“名词”,所以我的猜测是“ j”被某种程度地破坏了,因为它必须必须从之间的“跳”起来。 0和147K至399K +。不幸的是,这已经过去了,所以我今晚无法继续更新,但是随时可以发表任何想法,明天早上我将通过电子邮件进行检查:)谢谢大家!

最佳答案

更改所有for测试条件

next!=','




next != ',' && i < str.length()


除了你的最后一个

for (;next!=','&&next!=' ';next=str.charAt(++i))


应该是

for (;next!=','&&next!=' '&&i+1<str.length();next=str.charAt(++i))


接下来,我会尝试 String.split(String regex)

String[] words = str.split(",\\s+);


您可以使用 words.length获得字数统计。要获得行数,请在调用 readLine()时增加一个计数器,例如

in = new In(synsets);
// for ( str=in.readLine();str!=null;str=in.readLine() )
while ((str = in.readLine()) != null) {
linecount++;
String[] words = str.split(",\\s+);
nouncount += words.length;
}

关于java - 堆重载JVM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29665401/

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