使用seque函数时的OutOfMemoryError

Vik*_* K. 4 clojure

我有这个功能,可以重现我的问题:

(defn my-problem
  [preprocess count print-freq]
  (doseq [x (preprocess (range 0 count))] 
    (when (= 0 (mod x print-freq)) 
      (println x))))
Run Code Online (Sandbox Code Playgroud)

当我用这样的身份函数调用它时,一切正常:

(my-problem identity 10000000 200000)
;it prints 200000,400000 ... 9800000 just as it should
Run Code Online (Sandbox Code Playgroud)

当我用seque函数调用它时,我得到OutOfMemoryError:

(my-problem #(seque 5 %) 10000000 200000)
;it prints numbers up to 2000000 and then it throws OutOfMemoryException
Run Code Online (Sandbox Code Playgroud)

我的理解是seque函数应该使用ConcurrentBlockingQueue将处理分成两个线程,最大大小为5(在本例中).我不明白内存泄漏在哪里.

ama*_*loy 6

seque实现的方式是,如果消耗的元素比生成元素的速度快得多,则大量代理任务将堆积在内部使用的队列中seque(序列中每个元素最多一个任务).从理论上讲,你所做的应该是好的,但在实践中它并没有真正成功.你应该能够通过跑步看到相同的效果(dorun (seque (range))).

您还可以sequeueflatland/useful中使用该函数,这会使得与clojure.core中的函数不同.仔细阅读文档字符串,但我认为它适用于您的情况.