Hi all, I am wondering is there a way we can measure the memory consumption of a function in Clojure.
Let’s say this is an algorithm for finding the Levenshtein Distance
You can often get a reasonably accurate memory picture by making a ‘time’ like macro that does a GC before invoking your function and then consults memory statistics afterward. It’s more complicated than that, and if a GC ran while your function was running it would likely invalidate any meaningful numbers. Still, I’ve done this for small things, it can usefully tell you, for example, how much memory is used by a particular allocation.
You can query various memory properties via the java runtime: e.g.
(Runtime/getRuntime), (ManagementFactory/getGarbageCollectorMXBeans).
(ManagementFactory/getMemoryMXBean), (ManagementFactory/getMemoryPoolMXBeans)
And then digging through the information they contained. I’ve used this in the past to emulate a common-lisp like (room t) function.