老是内存溢出,java.lang.OutOfMemoryError

小带鱼 2008-10-20 02:11:32
用Eclipse编写了一个程序,涉及到在文本文件中读取数据进行处理,数据量一超过就会提示java.lang.OutOfMemoryError,减少数据量就没问题,我已经扩展了2G内存,请问如何让我的程序能够使用更多的内存去执行?我试过设置eclipse的eclipse.ini中的
-vmargs
-Xms512m
-Xmx1024m
这样改了还是不行。。请问这个问题怎么解决
...全文
1278 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
kissinger2000 2009-06-15
  • 打赏
  • 举报
回复
使用分批读取数据的办法,比如你5万条数据可以分成5个一万条读取,还有就是数据读取完及时设置成null这样垃圾回收器可以及时回收内存。
jyz3051 2008-10-21
  • 打赏
  • 举报
回复
首先,你得确认一下你的这两个参数是不是真正的改过来了,确认方法是在Doc环境下用命令行,具体的命令随便baidu或Google一下就可以了。因为你在一个地方改了这个参数,并不能保证JVM获得的就是这个值,要保证你使用的JVM是使用的这个值。
如果你确认了,这个参数确实是你设置的值,那么你就得检查你的代码了,据我的经验1G的内存能够存放的数据量,一般是很难达到的。故你经常溢出,很可能就是你代码写的有问题。
小带鱼 2008-10-21
  • 打赏
  • 举报
回复
我看到了,但是我还是不知道是哪个,真不好意思xuhaiyang ,能不能请您再说详细一点,里面很多参数,我也不知道哪个才是设置内存的那个
xuhaiyang 2008-10-20
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 dawnspace 的回复:]
xuhaiyang
说的设置应用程序内存能说具体一点吗?我没看到里面有设置这个的啊
[/Quote]

点开run,就可以看见配置页面,选择Arguments,然后在VM Arguments里面就可以输入你的虚拟机参数了
Sou2012 2008-10-20
  • 打赏
  • 举报
回复
代码有问题吧..
yjfjebj789 2008-10-20
  • 打赏
  • 举报
回复
在环境变量里面设置吧
jcyan 2008-10-20
  • 打赏
  • 举报
回复
所有的涉及到资源的释放都要放到finally里(如,释放链接,文件流的关闭)
你给的两段代码里所有的reader.close()都要这样修改
还有,你使用jml操作了excel,那些workbook最终close了没?
小带鱼 2008-10-20
  • 打赏
  • 举报
回复
xuhaiyang
说的设置应用程序内存能说具体一点吗?我没看到里面有设置这个的啊
GGMMYQL 2008-10-20
  • 打赏
  • 举报
回复
兄弟,你的这些close请放到finally块里 好吗?
小带鱼 2008-10-20
  • 打赏
  • 举报
回复

package zlqrdmjg;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Set;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class Cndata {

/**
* @param args
*/
static ArrayList countrys = new ArrayList();
static ArrayList names_zh = new ArrayList();
static ArrayList names = new ArrayList();
static ArrayList names_en = new ArrayList();
static ArrayList kinds = new ArrayList();
static ArrayList oricodes = new ArrayList();



public static ArrayList cnOricode() {
ArrayList oricode = new ArrayList();
String line;
try {
BufferedReader reader = new BufferedReader(new FileReader(
"jigedaima.TXT"));
while ((line = reader.readLine()) != null) {
// System.out.println(line);
oricode.add(line.trim());
}
reader.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return oricode;
}

public static ArrayList cnCountry() {
ArrayList country = new ArrayList();
String line;
try {
BufferedReader reader = new BufferedReader(new FileReader(
"guobie.TXT"));
while ((line = reader.readLine()) != null) {
// System.out.println(line);
country.add(line.trim());
}
reader.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return country;
}

public static ArrayList cnName_en() {
ArrayList name_en = new ArrayList();
String line;
try {
BufferedReader reader = new BufferedReader(new FileReader(
"ywmc.TXT"));
while ((line = reader.readLine()) != null) {
// System.out.println(line);
name_en.add(line.trim());
}
reader.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return name_en;
}
public static ArrayList cnName_zh() {
ArrayList name_zh = new ArrayList();
String line;
try {
BufferedReader reader = new BufferedReader(new FileReader("zwmc.TXT"));
while ((line = reader.readLine()) != null) {
// System.out.println(line);
name_zh.add(line.trim());
}
reader.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return name_zh;
}

public static ArrayList cnKind() {
ArrayList kind = new ArrayList();
String line;
try {
BufferedReader reader = new BufferedReader(new FileReader(
"leixing.TXT"));
while ((line = reader.readLine()) != null) {
// System.out.println(line);
kind.add(line.trim());
}
reader.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return kind;
}
public static HashMap getNames(String name_en,String name_zh){
HashMap namemap = new HashMap();
String[] names_en = name_en.split(";");
String[] names_zh = name_zh.split(";");
for(int i = 0;i < names_en.length;i++){
if(!names_en[i].equals(""))
namemap.put(names_en[i],"EN" );
}
for(int i = 0;i < names_zh.length;i++){
if(!names_zh[i].equals(""))
namemap.put(names_zh[i],"ZH" );
}

return namemap;
}

public static ArrayList nameTotals() {
ArrayList nametotals = new ArrayList();
ArrayList nametotal;
HashMap namemap;
String name,oricode, sid, country, lang, type, kind;
//readExcel();
sid = "";
type = "CN";
oricodes = cnOricode();
countrys = cnCountry();
kinds = cnKind();
names_en = cnName_en();
names_zh = cnName_zh();
for (int i = 0; i < oricodes.size(); i++) {

oricode = (String) oricodes.get(i);
country = (String) countrys.get(i);
kind = (String) kinds.get(i);
//namemap = (HashMap) names.get(i);
namemap = getNames((String)names_en.get(i),(String)names_zh.get(i));
Set keyset = namemap.keySet();
Iterator iter = keyset.iterator();
while (iter.hasNext()) {
nametotal = new ArrayList();
name = (String)iter.next();
lang = (String)namemap.get(name);
nametotal.add(name);
nametotal.add(oricode);
nametotal.add(sid);
nametotal.add(country);
nametotal.add(lang);
nametotal.add(type);
nametotal.add(kind);
nametotals.add(nametotal);
}
}
return nametotals;
}


}

是这个,上面的那段代码有问题
小带鱼 2008-10-20
  • 打赏
  • 举报
回复
错了错了,上面的程序发错了。不是这个
一洽客服系统 2008-10-20
  • 打赏
  • 举报
回复
增大内存,及时关闭流,使用buffer读写
小带鱼 2008-10-20
  • 打赏
  • 举报
回复
package zlqrdmjg;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;

import jxl.Cell;
import jxl.Sheet;
import jxl.Workbook;

public class C3data {

/**
* @param args
*/

static ArrayList countrys = new ArrayList();
static ArrayList kinds = new ArrayList();
static ArrayList names = new ArrayList();

public static ArrayList c3Country() {
ArrayList country = new ArrayList();
String line;
try {
BufferedReader reader = new BufferedReader(new FileReader(
"1561C3数据-国别.txt"));
while ((line = reader.readLine()) != null) {
// System.out.println(line);
country.add(line.trim());
}
reader.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return country;
}

public static ArrayList c3Kind() {
ArrayList kind = new ArrayList();
String line;
try {
BufferedReader reader = new BufferedReader(new FileReader(
"1561C3数据-机构类型.txt"));
while ((line = reader.readLine()) != null) {
// System.out.println(line);
kind.add(line.trim());
}
reader.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return kind;
}

public static ArrayList c3Name() {
ArrayList name = new ArrayList();
String line;
try {
BufferedReader reader = new BufferedReader(new FileReader(
"1561C3数据-机构名称.txt"));
while ((line = reader.readLine()) != null) {
// System.out.println(line);
name.add(line.trim());
}
reader.close();

} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return name;
}

public static ArrayList nameTotals() {
ArrayList nametotals = new ArrayList();
ArrayList nametotal,nametotalmapping;
String name, oricode, sid, country, lang, type, kind;
readExcel();
if (names.size() != kinds.size()) {
System.out.println("样例DOCDB跟DOCBA数量不一致!");
return null;
}
if (names.size() != countrys.size()) {
System.out.println("样例DOCDB跟DOCBA数量不一致!");
return null;
}
oricode = "";
sid = "";
lang = "ZH";
type = "C3";
nametotalmapping = new ArrayList();
for (int i = 0; i < names.size(); i++) {
name = (String) names.get(i);
if(nametotalmapping.contains(name)){
continue;
}
nametotalmapping.add(name);
nametotal = new ArrayList();
country = (String)countrys.get(i);
kind = (String)kinds.get(i);
nametotal.add(name);
nametotal.add(oricode);
nametotal.add(sid);
nametotal.add(country);
nametotal.add(lang);
nametotal.add(type);
nametotal.add(kind);
nametotals.add(nametotal);
}
return nametotals;
}


}

5个文本文件中都含有6万条数据,这才是其中一个类。我一共有5个这样的类,一次性运算,中数据量达到30万条数据
xuhaiyang 2008-10-20
  • 打赏
  • 举报
回复
eclipse.ini设置的是eclipse占用的内存,不是你应用程序

你可以在每个应用程序的启动项里设置。就是run小三角旁边的下拉箭头,然后点run...就可以设置了
nine_suns99 2008-10-20
  • 打赏
  • 举报
回复
是不是读取了文件以后没有关闭输入流?
jcyan 2008-10-20
  • 打赏
  • 举报
回复
上代码给俺们瞅瞅
GGMMYQL 2008-10-20
  • 打赏
  • 举报
回复
还是查查你的程序吧

62,615

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧