How to use .getObjectSize in Clojure?

139 Views Asked by At

In Clojure, what is the best way to determine the size of a map? Import java library "java.lang.instrument.Instrumentation" and use .getObjectSize. However, the library cannot be imported. Please comment how to import the library correctly.

; Load the instrumentation namespace to get access to its vars
(require '[java.lang.instrument.Instrumentation :as inst])

; Find the size of an object
(println "The size of a map is"
         (inst/getObjectSize {:ts 20200101170000219 :bid 108.730000 :ask 108.786000})
         "bytes")
1

There are 1 best solutions below

1
andy_fingerhut On

I have not used that Java library before, but note that many collections in Clojure are implemented using many Java objects, i.e. typically a "root" object that points to many other objects. If you want to know the size of all of these, and/or create drawings showing their structure, I have created a library called cljol that can do this: https://github.com/jafingerhut/cljol

The README shows examples of use and the output you can expect. There is also a link in the top level README to a gallery of other images created using the library, for a few Clojure data structures.