In Clojure, how can I determine how much memory ref uses?

I want to start defining the memory requirements of my links and how they grow using the application. How can i do this?

+4
source share
3 answers

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.

+4
source

Someone asked about this on the mailing list some time ago (and probably someone else before that and ...). Several people have provided utilities that seem to do what you might need, but I still prefer my answer: you cannot do this in a language with such a widespread and automatic structure separation. How do you calculate the size of a large object to which you have two pointers, etc. Etc.

+5
source

Because I can not do the code in the comments:

(let [a1 large-hash-map] [a2 (assoc large-hash-map :foo :bar)] ;; now, the two 'pointers' are a1 and a2 ;; and the data structures they point to can share ;; most (but not all) of their data. ;; making it more or less meaningless to ask ;; how much memory any of the bindings holds ) 

Whether we're talking about links or simple bindings, it doesn't matter how much your question is.

+3
source

Source: https://habr.com/ru/post/1346692/


All Articles