Skip to content

Instantly share code, notes, and snippets.

@Hamled
Last active July 2, 2020 14:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Hamled/cab41245abd208b4f6cb67026f135e34 to your computer and use it in GitHub Desktop.
Save Hamled/cab41245abd208b4f6cb67026f135e34 to your computer and use it in GitHub Desktop.
(ns buffers-test
(:require [manifold.deferred :as d]))
(defn buffers-for [n size]
(for [n (range n)]
(java.nio.ByteBuffer/allocate size)))
(defn buffers [size]
(lazy-seq
(cons (java.nio.ByteBuffer/allocate size)
(buffers size))))
(defn loop-buffers [bs]
(loop [bs bs]
(when-not (empty? bs)
(println (.capacity (first bs)))
(recur (rest bs)))))
(defn d-loop-buffers [bs]
@(d/loop [bs bs]
(when-not (empty? bs)
(println (.capacity (first bs)))
(d/recur (rest bs)))))
(defn d-loop-buffers-fut [bs]
(d/future
@(d/loop [bs bs]
(when-not (empty? bs)
(println (.capacity (first bs)))
(d/recur (rest bs))))))
; Run this with -Xmx1G
; or otherwise set SIZE to a value so that
; 10 * SIZE bytes is enough to OOM
(def SIZE (* 250 1024 1024))
; These work fine
(loop-buffers (take 10 (buffers SIZE)))
(d-loop-buffers (take 10 (buffers SIZE)))
@(d/future (d-loop-buffers (take 10 (buffers SIZE))))
; These result in OOM
(loop-buffers (buffers-for 10 SIZE))
(d-loop-buffers (buffers-for 10 SIZE))
@(d-loop-buffers-fut (take 10 (buffers SIZE)))
@(d-loop-buffers-fut (buffers-for 10 SIZE))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment