so something like:
(total-memory [1 2 [1 2]])
would return however many bytes this structure is.
Is there already something like this around? Would it be hard to write
to work for any clojure data structure?
just like (time), I think (total-memory) would be very enlightening
for basic testing.
sincerely,
--Robert McIntyre
> I think it would be really cool to have a function that gives the
> total number of bytes that a data structure consumes.
>
> so something like:
>
> (total-memory [1 2 [1 2]])
>
> would return however many bytes this structure is.
>
> Is there already something like this around? Would it be hard to write
> to work for any clojure data structure?
I've used the following serialization trick during development in a java
context:
(defn total-memory [obj]
(let [baos (java.io.ByteArrayOutputStream.)]
(with-open [oos (java.io.ObjectOutputStream. baos)]
(.writeObject oos obj))
(count (.toByteArray baos))))
It is *very* inaccurate but gives some indication of size.
Regards,
Remco
--
Mikhail