gpt4 book ai didi

java - 我收到内存不足错误 : Java heap space Exception

转载 作者:行者123 更新时间:2023-11-30 08:58:44 25 4
gpt4 key购买 nike

我目前正在尝试接收一个文本文件并将文件中的每个单词读入二叉树,我得到的具体错误是:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

我正在读入项目的文本文件是教授给我的作业所以我知道这不应该遇到任何内存问题我以前从未遇到过这种类型的异常并且不知道在哪里开始请帮助。这是我的代码:

public class Tester {
public static void main(String[] args) throws FileNotFoundException {
Tester run = new Tester();
run.it();
}

public void it() throws FileNotFoundException {
BTree theTree = new BTree();
String str = this.readInFile();
String [] firstWords = this.breakIntoWords(str);
String [] finalWords = this.removeNullValues(firstWords);

for(int i = 0; i < finalWords.length; i++) {
theTree.add(finalWords[i]);
}
theTree.print();
}

public String readInFile() throws FileNotFoundException {
String myFile = "";
int numWords = 0;
Scanner myScan = new Scanner(new File("Dracula.txt"));

while(myScan.hasNext() == true) {
myFile += myScan.nextLine() + " ";
}
return myFile;
}

public String [] breakIntoWords(String myFile) {
String[] words = new String[myFile.length()];
String nextWord = "";
int position = 0;
int i = 0;

while(myFile.length() > position) {
char next = myFile.charAt(position);
next = Character.toLowerCase(next);

// First trim beginning
while (((next < 'a') || (next > 'z')) && !Character.isDigit(next)) {
position++;
next = myFile.charAt(position);
next = Character.toLowerCase(next);
}

// Now pull only letters or numbers until we hit a space
while(!Character.isWhitespace(next)) {
if (Character.isLetterOrDigit(next)) {
nextWord += myFile.charAt(position);
}
position++;
next = myFile.charAt(position);
}
words [i] = nextWord;
i++;
}
return words;
}

public String[] removeNullValues(String[] myWords) {
String[] justMyWords = new String[myWords.length];
for (int i = 0; i < myWords.length; i++) {
if (myWords[i] != null) {
justMyWords[i] = myWords[i];
}
}
return justMyWords;
}
}

这是我的 B 树类:

public class BTree {
private BTNode root;
private int nodeCount;

public boolean add(String word) {
BTNode myNode = new BTNode(word);

if(root == null) {
root = myNode;
nodeCount++;
return true;
}

if(findNode(word)) {
int tmp = myNode.getNumInstance();
tmp++;
myNode.setNumInstance(tmp);
return false;
}

BTNode temp = root;
while(temp != null) {
if(word.compareTo(temp.getMyWord()) < 0) {
if(temp.getRightChild() == null) {
temp.setLeftChild(myNode);
nodeCount++;
return true;
} else {
temp = temp.getRightChild();
}
} else {
if(temp.getLeftChild() == null) {
temp.setLeftChild(myNode);
nodeCount++;
return true;
} else {
temp = temp.getLeftChild();
}
}
}
return false;
}

public boolean findNode(String word) {
return mySearch(root, word);
}

private boolean mySearch(BTNode root, String word) {
if (root == null) {
return false;
}
if ((root.getMyWord().compareTo(word) < 0)) {
return true;
} else {
if (word.compareTo(root.getMyWord()) > 0) {
return mySearch(root.getLeftChild(), word);
} else {
return mySearch(root.getRightChild(), word);
}
}
}

public void print() {
printTree(root);
}

private void printTree(BTNode root) {
if (root == null) {
System.out.print(".");
return;
}
printTree(root.getLeftChild());
System.out.print(root.getMyWord());
printTree(root.getRightChild());

}

public int wordCount() {
return nodeCount;
}
}

还有我的 B 树节点类:

public class BTNode {
private BTNode rightChild;
private BTNode leftChild;
private String myWord;
private int numWords;
private int numInstance;
private boolean uniqueWord;
private boolean isRoot;
private boolean isDeepest;

public BTNode(String myWord){

this.numInstance = 1;
this.myWord = myWord;
this.rightChild = null;
this.leftChild = null;

}

public String getMyWord() {
return myWord;
}

public void setMyWord(String myWord) {
this.myWord = myWord;
}

public BTNode getRightChild() {
return rightChild;
}

public void setRightChild(BTNode rightChild) {
this.rightChild = rightChild;
}

public BTNode getLeftChild() {
return leftChild;
}

public void setLeftChild(BTNode leftChild) {
this.leftChild = leftChild;
}

public int getnumWords() {
return numWords;
}

public void setnumWords(int numWords) {
this.numWords = numWords;
}

public boolean isUniqueWord() {
return uniqueWord;
}

public void setUniqueWord(boolean uniqueWord) {
this.uniqueWord = uniqueWord;
}

public boolean isRoot() {
return isRoot;
}

public void setRoot(boolean isRoot) {
this.isRoot = isRoot;
}

public boolean isDeepest() {
return isDeepest;
}

public void setDeepest(boolean isDeepest) {
this.isDeepest = isDeepest;
}

public int getNumInstance() {
return numInstance;
}

public void setNumInstance(int numInstance) {
this.numInstance = numInstance;
}
}

最佳答案

这个小文件不应该是 OutOfMemory 错误的原因。

性能那没有错误,但是如果你想读取内存中的整个文件
不要逐行读取并连接字符串。这会减慢您的程序。
您可以使用:

String myFile = new String(Files.readAllBytes(Paths.get("Dracula.txt")));
myFile = myFile.replaceAll("\r\n", " ");
return myFile;

这也不是超快,而是更快。

现在是错误

单词数组太大

public String[] breakIntoWords(String myFile) {
String[] words = new String[myFile.length()];

您将单词定义为长度为 文件长度 的数组。那太大了,如果你这个名字是助记的,意味着你需要一个长度为 count of words in file

的数组

nextWord 永远不会重置(OutOfMemory 的原因)

        // Now pull only letters or numbers until we hit a space
while (!Character.isWhitespace(next)) {
if (Character.isLetterOrDigit(next)) {
nextWord += myFile.charAt(position);
}
position++;
next = myFile.charAt(position);
}
words[i] = nextWord;
i++;

因为下一个词在分配给 words[i] 后永远不会设置为 ""。以便下一个单词增长
一个字一个字地向上排列,你的数组内容如下所示:

words[0] = "Word1"
words[1] = "Word1Word2"
words[2] = "Word1Word2Word3"

如您所想,这将导致大量已用空间。

关于java - 我收到内存不足错误 : Java heap space Exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27509907/

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