scala:反序列化hashmap

时间:2013-02-27 19:41:14

标签: scala serialization hashmap

我正在尝试在scala中序列化和反序列化hashmap 但是当我尝试加载我的hashmap时,我有这样的错误:“java.lang.OutOfMemoryError:Java堆空间”(序列化数据大小接近8 MiB,程序在100KiB附近的大小上工作得很好)。

(我是scala中的新手,也许这段代码很难看)

case class StoredPage (URL : String, page_html : String, keyWords : scala.collection.mutable.HashMap[String, Int], links : List[String], images : List[String], hash : Array[Byte])
/*......*/
def SerialisePages(fname : String, foo : scala.collection.mutable.HashMap[String, StoredPage]) = {
    val out = new FileOutputStream(fname)
    out.write(Marshal.dump(foo.values.toList))
    out.close
}

def DeSerialisePages(fname : String) = {
    val in = new FileInputStream(fname)
    val bytes = Stream.continually(in.read).takeWhile(-1 !=).map(_.toByte).toArray
    val lst = Marshal.load[List[StoredPage]](bytes)
    var pages = scala.collection.mutable.HashMap[String, StoredPage]()
    for(p <- lst) {
        pages.put(p.URL, p)
    }
    pages
}
/*...........*/
//Create 
var pages = scala.collection.mutable.HashMap[String, StoredPage]()
/*some work*/
SerialisePages("index", pages) //is OK
//replace var pages = scala.collection.mutable.HashMap[String, StoredPage]()
//Run program again
var pages = scala.collection.mutable.HashMap[String, StoredPage]()//Error

完整的错误消息:

java.lang.OutOfMemoryError: Java heap space
    at scala.collection.immutable.Stream.takeWhile(Stream.scala:782)
    at scala.collection.immutable.Stream$$anonfun$takeWhile$1.apply(Stream.scala:782)
    at scala.collection.immutable.Stream$$anonfun$takeWhile$1.apply(Stream.scala:782)
    at scala.collection.immutable.Stream$Cons.tail(Stream.scala:1060)
    at scala.collection.immutable.Stream$Cons.tail(Stream.scala:1052)
    at scala.collection.immutable.Stream$$anonfun$map$1.apply(Stream.scala:376)
    at scala.collection.immutable.Stream$$anonfun$map$1.apply(Stream.scala:376)
    at scala.collection.immutable.Stream$Cons.tail(Stream.scala:1060)
    at scala.collection.immutable.Stream$Cons.tail(Stream.scala:1052)
    at scala.collection.immutable.Stream.length(Stream.scala:284)
    at scala.collection.SeqLike$class.size(SeqLike.scala:100)
    at scala.collection.immutable.Stream.size(Stream.scala:186)
    at scala.collection.TraversableOnce$class.toArray(TraversableOnce.scala:233)
    at scala.collection.immutable.Stream.toArray(Stream.scala:186)
    at Appp$.DeSerialisePages(crawler.scala:94)
    at Appp$.grabHost$1(crawler.scala:109)
    at Appp$.main(crawler.scala:132)
    at Appp.main(crawler.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:601)
    at scala.tools.nsc.util.ScalaClassLoader$$anonfun$run$1.apply(ScalaClassLoader.scala:78)
    at scala.tools.nsc.util.ScalaClassLoader$class.asContext(ScalaClassLoader.scala:24)
    at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.asContext(ScalaClassLoader.scala:88)
    at scala.tools.nsc.util.ScalaClassLoader$class.run(ScalaClassLoader.scala:78)
    at scala.tools.nsc.util.ScalaClassLoader$URLClassLoader.run(ScalaClassLoader.scala:101)
    at scala.tools.nsc.ObjectRunner$.run(ObjectRunner.scala:33)
    at scala.tools.nsc.ObjectRunner$.runAndCatch(ObjectRunner.scala:40)
    at scala.tools.nsc.MainGenericRunner.runTarget$1(MainGenericRunner.scala:56)
    at scala.tools.nsc.MainGenericRunner.process(MainGenericRunner.scala:80)
    at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:89)

有出路吗?

也许还有另一种简单的方法来序列化哈希表?

Scala ver。:2.9.2操作系统:Ubuntu

对不起我的写作错误。英语不是我的母语

0 个答案:

没有答案