Skip to content

Instantly share code, notes, and snippets.

@jcromartie jcromartie/mem.clj
Created Nov 3, 2009

Embed
What would you like to do?
Macro to measure memory usage of an expr
(defn bytes-used
"Returns bytes used by Java VM (after collecting garbage)"
[]
(let [rt (Runtime/getRuntime)]
(dotimes [_ 5] (System/gc))
(- (.totalMemory rt) (.freeMemory rt))))
(defmacro mime
"Measures and prints the bytes used while evaluating expr (returns result of expr)"
[expr]
`(let [mem-start# (bytes-used)
result# (do ~expr)]
(println (- (bytes-used) mem-start#) "bytes used")
result#))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.