背景
正如这个问题所指出的那样,我正在使用Scalaz 7 iteratees在恒定堆空间中处理大量(即无界)数据流。
我的代码看起来像这样:
type ErrorOrT[M[+_], A] = EitherT[M, Throwable, A]
type ErrorOr[A] = ErrorOrT[IO, A]
def processChunk(c: Chunk, idx: Long): Result
def process(data: EnumeratorT[Chunk, ErrorOr]): IterateeT[Vector[(Chunk, Long)], ErrorOr, Vector[Result]] =
Iteratee.fold[Vector[(Chunk, Long)], ErrorOr, Vector[Result]](Nil) { (rs, vs) =>
rs ++ vs map {
case (c, i) => processChunk(c, i)
}
} &= (data.zipWithIndex mapE Iteratee.group(P))
问题
我似乎遇到了内存泄漏问题,但我对Scalaz/FP不够熟悉,无法确定错误是在Scalaz还是在我的代码中。直觉上,我希望这段代码仅需要大约P倍于Chunk
大小的空间。
注意:我在类似的问题中发现了一个OutOfMemoryError
,但我的代码没有使用consume
。
我运行了一些测试来尝试隔离问题。总结起来,只有在同时使用zipWithIndex
和group
时才会出现内存泄漏。
// no zipping/grouping
scala> (i1 &= enumArrs(1 << 25, 128)).run.unsafePerformIO
res47: Long = 4294967296
// grouping only
scala> (i2 &= (enumArrs(1 << 25, 128) mapE Iteratee.group(4))).run.unsafePerformIO
res49: Long = 4294967296
// zipping and grouping
scala> (i3 &= (enumArrs(1 << 25, 128).zipWithIndex mapE Iteratee.group(4))).run.unsafePerformIO
java.lang.OutOfMemoryError: Java heap space
// zipping only
scala> (i4 &= (enumArrs(1 << 25, 128).zipWithIndex)).run.unsafePerformIO
res51: Long = 4294967296
// no zipping/grouping, larger arrays
scala> (i1 &= enumArrs(1 << 27, 128)).run.unsafePerformIO
res53: Long = 17179869184
// zipping only, larger arrays
scala> (i4 &= (enumArrs(1 << 27, 128).zipWithIndex)).run.unsafePerformIO
res54: Long = 17179869184
测试用例的代码:
import scalaz.iteratee._, scalaz.effect.IO, scalaz.std.vector._
// define an enumerator that produces a stream of new, zero-filled arrays
def enumArrs(sz: Int, n: Int) =
Iteratee.enumIterator[Array[Int], IO](
Iterator.continually(Array.fill(sz)(0)).take(n))
// define an iteratee that consumes a stream of arrays
// and computes its length
val i1 = Iteratee.fold[Array[Int], IO, Long](0) {
(c, a) => c + a.length
}
// define an iteratee that consumes a grouped stream of arrays
// and computes its length
val i2 = Iteratee.fold[Vector[Array[Int]], IO, Long](0) {
(c, as) => c + as.map(_.length).sum
}
// define an iteratee that consumes a grouped/zipped stream of arrays
// and computes its length
val i3 = Iteratee.fold[Vector[(Array[Int], Long)], IO, Long](0) {
(c, vs) => c + vs.map(_._1.length).sum
}
// define an iteratee that consumes a zipped stream of arrays
// and computes its length
val i4 = Iteratee.fold[(Array[Int], Long), IO, Long](0) {
(c, v) => c + v._1.length
}
问题
- 我的代码有 bug 吗?
- 如何使这个程序在常量堆空间中运行?
-XX:+HeapDumpOnOutOfMemoryError
并дҪҝз”ЁEclipse MAT http://www.eclipse.org/mat/еҲҶжһҗиҪ¬еӮЁпјҢд»ҘжҹҘзңӢе“ӘиЎҢд»Јз ҒжӯЈеңЁдҝқз•ҷж•°з»„гҖӮ - huynhjlvar
计数器,而不需要那个特定的函数式编程结构来复制zipWithIndex。 - Ezekiel VictorLong
索引会将算法从常量堆空间更改为非常量堆空间吗?非压缩版本显然使用恒定的堆空间,因为它可以“处理”您愿意等待的任意数量的块。 - Aaron Novstrup