Skip to content

Instantly share code, notes, and snippets.

@jcromartie
Created November 3, 2009 19:56
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 jcromartie/225372 to your computer and use it in GitHub Desktop.
Save jcromartie/225372 to your computer and use it in GitHub Desktop.
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