简体   繁体   English

Clojure关键字的内存大小是多少?

[英]What is the size in memory of a Clojure keyword?

I'm writing a function in Clojure to estimate the in-memory size of a parsed JSON, something like: 我正在Clojure中编写一个函数来估计解析的JSON的内存大小,如:

(defn object-size
  [object]
  (cond
    (sequential? object)
      (reduce + (map object-size object))
    (map? object)
      (reduce
        (fn [total [k v]]
          (+ total (keyword-size k) (object-size v)))
        0
        object)
    :else
      (case (type object)
        java.lang.Long 8 
        java.lang.Double 8
        java.lang.String (* 2 (count object))
        ;; other data types
      )))

Obviously I'll need to add in overheads for clojure.lang.PersistentVector , java.lang.String , etc. 显然我需要为clojure.lang.PersistentVectorjava.lang.String等添加开销。

However, I'm not sure how to find the in-memory size of a clojure.lang.Keyword , the keyword-size function in the above example. 但是,我不确定如何找到clojure.lang.Keyword的内存大小,这是上例中的keyword-size函数。 How does Clojure store keywords? Clojure如何存储关键字? Are they constant size similar to a C++ enum , or are they a special case of java.lang.String that are dependent on length? 它们的大小是否与C ++ enum类似,或者它们是依赖于长度的java.lang.String的特例?

Answering this question from within Clojure is basically impossible. 从Clojure中回答这个问题基本上是不可能的。 Your first-draft function works okay for the very simplest data structures, although even this simplest attempt has several errors already. 你的函数首先,草案的非常简单的数据结构,工作 ,但即使是这样简单的尝试有几个错误了。

But more than that, it is just an ill-framed question. 但更重要的是,这只是一个框架错误的问题。 What is the size of xs in this snippet? 此代码段中xs的大小是多少?

(def xs (let [forever (promise)]
          (deliver forever
                   (lazy-seq (cons 1 @forever)))
          @forever))

user=> (take 5 xs)
(1 1 1 1 1)

xs is an infinitely long sequence (so your reduce will never complete, but if it could it would surely return "this is infinite"). xs是一个无限长的序列(因此你的reduce将永远不会完成,但如果它可能肯定会返回“这是无限的”)。 But it actually takes a small, fixed amount of memory, because it is circular. 但它实际上需要一小部分固定的内存,因为它是循环的。

You may say, well gee this is a dumb object, I don't mind if my function fails for objects like that. 你可能会说,这是一个愚蠢的对象,我不介意我的功能是否因为这样的对象而失败。 But in a garbage-collected language with pervasive laziness, cases with similar characteristics are commonplace. 但是,在一种普遍存在的懒惰的垃圾收集语言中,具有相似特征的案例是司空见惯的。 If you rule them out, you rule out everything interesting. 如果你排除它们,你就排除了一切有趣的东西。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM