On 2010/12/23 05:51, Robert McIntyre wrote:

> 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

-- 
You received this message because you are subscribed to the Google
Groups "Clojure" group.
To post to this group, send email to [email protected]
Note that posts from new members are moderated - please be patient with your 
first post.
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/clojure?hl=en

Reply via email to