我生成了一个hashmap,生成文件到硬盘中存贮,显示大小为2.2G
tomcat启动时需要把该hashmap读入内存中。
tomcat的java虚拟机开到了9G,可是读此文件时,竟然内存溢出了,谁知道什么原因,谢谢了。
写文件和读文件的代码见下:hashmap 是 类似这样的结构:key:中国 value:是ArrayList, list(0)=中国人,list(1)=中国人民,list(2)=中国人大。。 // 写入文件
public static void write(Object o, String filename) {
try {
//File file=new File(filename);
// if(!file.exists()){
//file.createNewFile();     
//}
ObjectOutputStream w = new ObjectOutputStream(new FileOutputStream(
filename));
w.writeObject(o);
w.flush();
w.close();
} catch (Exception e) {
e.printStackTrace();
}
} // 读取文件
public static Object Reader(String file) {
try {
ObjectInputStream in = new ObjectInputStream(new FileInputStream(
file));
Object o = in.readObject();
in.close();
return o;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}