java.lang.OutOfMemoryError: Java heap space错误~如何解决~~?

judking judking 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
我在写个project~关于词典方面的,需要读入数据(数据大小约27M),然后我用bufferedReader将数据每行转成字符串存在String[]数组里~~~~当我调试程序的时候就出现了如题的错误。搜索了下相关知识,虽然可以调heap space初始大小,但终究不是解决问题的根本途径(别人电脑上的初始值还是没变啊~~)请问大家应该如何处理,我这样读入数据的方式怎样做可以更好一些??(因为为了即时性(速度),所以读入到String数组中,而不是每次查词都打开文件、读入文件再进行搜索。)
请大家赐教~~感激不尽~~~~~~~~~~~~~~~~~~~~~~~~~~下附出错的相关源代码:

这是我用于读取文件的类

public class Location {
private String[] s;
private int num_line = 0;
private int real_start = 0; //前面声明的行数
//(最终real_start对应的值即为第一行有用数据在s数组中的下标)

//初始化File中的每一行字符串,并存入s数组中
public Location(File f) throws IOException {
//统计行数
BufferedReader br = new BufferedReader(new FileReader(f));
while(br.readLine() != null) {
++num_line;
}
br.close();
//存入到s数组中
br = new BufferedReader(new FileReader(f));
s = new String[num_line+1];
int cnt = 0; // 当前处理的行
while((s[cnt] = br.readLine()) != null) {
if(s[cnt].startsWith(" ")) {
++real_start;
}
++cnt;
}
br.close();
}


在另一个类中连续调用Location从而读入数据(总计约27M)

public void init() throws IOException{
index_noun = new Location(new File("./dict/index.noun"));
index_verb = new Location(new File("./dict/index.verb"));
index_adj = new Location(new File("./dict/index.adj"));
index_adv = new Location(new File("./dict/index.adv"));
data_noun = new Location(new File("./dict/data.noun"));
data_verb = new Location(new File("./dict/data.verb"));
data_adj = new Location(new File("./dict/data.adj"));
data_adv = new Location(new File("./dict/data.adv"));
lex_file = new Location(new File("./dict/lex_file.txt"));
}
...全文

相关推荐

评论 11
收藏 已收藏
分享
返回顶部
judking
Java SE 62,621 成员 307,251 内容