In general, this is not a very useful thing: because the data used by any one ref is most likely to be used by many other refs, knowing that this information is not particularly useful.
It will also be very specific to JVMs - different JVM implementations may use different amounts of memory for the same Clojure structures, depending on how they select batch data structures and pointers. For example, I believe HotSpot objects are up to 8 bytes in size, but other JVMs can do something completely different. Also, 32/64-bit JVMs usually use different sizes for pointers (but not necessarily in an obvious way, since some 64-bit JVMs use compressed pointers ....)
If you still decide to do this, the best approach would probably be to recursively lower the data structure to ref and add the estimated size of each subitem.
- You will need to make assumptions or experimentally check the size / overhead of each possible type of component. Not easy ... see this question for some details on sizing objects on the JVM. If you're lucky, you can find a library that does this for you.
- You will also need to keep track of all the objects viewed, which is also difficult, since you need to compare the identifier of the object rather than equality, and therefore you cannot use any standard hash file / set. Hashmap will work (hashcode object -> collection of objects with the same hash code).
- There will also be some funny Clojure-specific corner cases to consider ... for example. Do you consider metadata of the data structure or not?
On average, however, I would recommend paying attention to the memory consumed by your application as a whole , rather than specific links.
source share