是否可以将函数式编程应用于Scala流,以便顺序处理流,但是可以对流中已处理的部分进行垃圾回收?
例如,我定义了一个Stream
,其中包含从start
到end
的数字:
def fromToStream(start: Int, end: Int) : Stream[Int] = {
if (end < start) Stream.empty
else start #:: fromToStream(start+1, end)
}
如果我以函数样式总结这些值:
println(fromToStream(1,10000000).reduceLeft(_+_))
我得到一个
OutOfMemoryError
-也许是因为对reduceLeft
的调用的堆栈框架包含对流头的引用。但是,如果我以迭代方式执行此操作,那么它将起作用:var sum = 0
for (i <- fromToStream(1,10000000)) {
sum += i
}
有没有一种方法可以在没有得到
OutOfMemory
的情况下以功能样式进行操作?更新:这是a bug in scala,现已修复。因此,这或多或少已经过时了。
最佳答案
是的你可以。诀窍是使用尾部递归方法,以便本地堆栈帧包含对Stream
实例的唯一引用。由于该方法是尾递归的,因此一旦它递归调用自身,对前一个Stream
头的本地引用将被删除,从而使GC可以在您进行操作时收集Stream
的开始。
Welcome to Scala version 2.9.0.r23459-b20101108091606 (Java HotSpot(TM) Server VM, Java 1.6.0_20).
Type in expressions to have them evaluated.
Type :help for more information.
scala> import collection.immutable.Stream
import collection.immutable.Stream
scala> import annotation.tailrec
import annotation.tailrec
scala> @tailrec def last(s: Stream[Int]): Int = if (s.tail.isEmpty) s.head else last(s.tail)
last: (s: scala.collection.immutable.Stream[Int])Int
scala> last(Stream.range(0, 100000000))
res2: Int = 99999999
另外,必须确保传递给上述
last
方法的内容在堆栈上仅具有一个引用。如果将Stream
存储到局部变量或值中,则在调用last
方法时不会将其垃圾回收,因为它的参数不是Stream
的唯一引用。下面的代码用完了内存。scala> val s = Stream.range(0, 100000000)
s: scala.collection.immutable.Stream[Int] = Stream(0, ?)
scala> last(s)
Exception in thread "main" java.lang.OutOfMemoryError: Java heap space
at sun.net.www.ParseUtil.encodePath(ParseUtil.java:84)
at sun.misc.URLClassPath$JarLoader.checkResource(URLClassPath.java:674)
at sun.misc.URLClassPath$JarLoader.getResource(URLClassPath.java:759)
at sun.misc.URLClassPath.getResource(URLClassPath.java:169)
at java.net.URLClassLoader$1.run(URLClassLoader.java:194)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:190)
at java.lang.ClassLoader.loadClass(ClassLoader.java:307)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:301)
at java.lang.ClassLoader.loadClass(ClassLoader.java:248)
at scala.tools.nsc.Interpreter$Request$$anonfun$onErr$1$1.apply(Interpreter.scala:978)
at scala.tools.nsc.Interpreter$Request$$anonfun$onErr$1$1.apply(Interpreter.scala:976)
at scala.util.control.Exception$Catch.apply(Exception.scala:80)
at scala.tools.nsc.Interpreter$Request.loadAndRun(Interpreter.scala:984)
at scala.tools.nsc.Interpreter.loadAndRunReq$1(Interpreter.scala:579)
at scala.tools.nsc.Interpreter.interpret(Interpreter.scala:599)
at scala.tools.nsc.Interpreter.interpret(Interpreter.scala:576)
at scala.tools.nsc.InterpreterLoop.reallyInterpret$1(InterpreterLoop.scala:472)
at scala.tools.nsc.InterpreterLoop.interpretStartingWith(InterpreterLoop.scala:515)
at scala.tools.nsc.InterpreterLoop.command(InterpreterLoop.scala:362)
at scala.tools.nsc.InterpreterLoop.processLine$1(InterpreterLoop.scala:243)
at scala.tools.nsc.InterpreterLoop.repl(InterpreterLoop.scala:249)
at scala.tools.nsc.InterpreterLoop.main(InterpreterLoop.scala:559)
at scala.tools.nsc.MainGenericRunner$.process(MainGenericRunner.scala:75)
at scala.tools.nsc.MainGenericRunner$.main(MainGenericRunner.scala:31)
at scala.tools.nsc.MainGenericRunner.main(MainGenericRunner.scala)
总结一下:
Stream
编辑:
请注意,这也有效,不会导致内存不足错误:
scala> def s = Stream.range(0, 100000000)
s: scala.collection.immutable.Stream[Int]
scala> last(s)
res1: Int = 99999999
编辑2:
如果需要
reduceLeft
,则必须使用结果的累加器参数定义一个辅助方法。对于reduceLeft,您需要一个累加器参数,您可以使用默认参数将其设置为某个值。一个简化的例子:
scala> @tailrec def rcl(s: Stream[Int], acc: Int = 0): Int = if (s.isEmpty) acc else rcl(s.tail, acc + s.head)
rcl: (s: scala.collection.immutable.Stream[Int],acc: Int)Int
scala> rcl(Stream.range(0, 10000000))
res6: Int = -2014260032
关于scala - Scala流的功能处理而不会出现OutOfMemory错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4132924/