Last active
April 30, 2019 22:05
-
-
Save sherpc/16e10aa2953c9e7a03da8ecc60dfe29a to your computer and use it in GitHub Desktop.
Clojure simple memory usage log
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(defn total-memory | |
[] | |
(.totalMemory (Runtime/getRuntime))) | |
(defn free-memory | |
[] | |
(.freeMemory (Runtime/getRuntime))) | |
(def steps | |
(->> ["B" "KB" "MB" "GB" "TB"] | |
(map vector (iterate #(* 1024.0 %) 1)) | |
reverse)) | |
(defn pprint-memory | |
[mem] | |
(some (fn [[s p]] | |
(when (> mem s) | |
(format "%.2f %s" (/ mem s) p))) | |
steps)) | |
(defn start-memory-log | |
"Usage: (start-memory-log \"memory.txt\" 5) -- log memory for five seconds, store result in file 'memory.txt'. Runs in non-blocking thread." | |
[out-path seconds-to-run] | |
(future | |
(->> (range seconds-to-run) | |
(map (fn [_] | |
(Thread/sleep 1000) | |
[(total-memory) (free-memory) (- (total-memory) (free-memory))])) | |
(into []) | |
(map (fn [m] (->> | |
m | |
(map pprint-memory) | |
(clojure.string/join ", ")))) | |
(clojure.string/join "\n") | |
(spit out-path)))) | |
(defn current-pid | |
[] | |
(-> (java.lang.management.ManagementFactory/getRuntimeMXBean) | |
(.getName) | |
(clojure.string/split #"@") | |
(first))) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment