普通Lisp中对象的内存使用情况 [英] memory usage by objects in common lisp

查看:159
本文介绍了普通Lisp中对象的内存使用情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有办法找出一个类的实例或基本数据类型通常使用多少内存?

Is there a way to find out how much memory is used by an instance of a class or basic data types in general?

我在cl中有一个玩具网络框架,它使用代表html标签及其属性的类实例来创建和管理网页,并且由于它们应该构成html页面,因此它们在称为子级的插槽中具有子级.因此,我在考虑如果采用这种方法,用户会话将使服务器花费多少钱.谢谢.

I have a toy webframework in cl that creates and manages web pages with instances of classes that represent the html tags and their properties, and as they are supposed to make an html page, they have children in a slot called children. so I was thinking how much a user's session will cost the server if I take this approach. Thanks.

推荐答案

据我所知,标准中的任意对象都没有像这样的东西,但是有依赖于实现的解决方案,例如CCL中的ccl:object-direct-size:

As far as I know, there is nothing like this for arbitrary objects in the standard, but there are implementation-dependent solutions, like ccl:object-direct-size in CCL:

CL-USER> (object-direct-size "foo")
16

但是,请注意,这些功能是否满足您的要求取决于大小"的含义,因为这些功能通常不包括对象引用的组件的大小.您还可以运行GC,初始化一些对象并比较 输出之前和之后.

However, be aware that whether these do what you want depends on what you mean by "size", since those functions usually don't include the size of the components the object references. You can also run the GC, initialize a few objects and compare room's output before and afterwards.

此外,请注意, time 通常包括分配信息:

Also, note that time usually includes allocation information:

CL-USER> (time (length (make-array 100000)))
(LENGTH (MAKE-ARRAY 100000))
took 0 milliseconds (0.000 seconds) to run.
During that period, and with 2 available CPU cores,
     0 milliseconds (0.000 seconds) were spent in user mode
     0 milliseconds (0.000 seconds) were spent in system mode
 400,040 bytes of memory allocated.
100000


也许您可以尝试这样的操作(未经测试,实际上只是一个快速的技巧):


Maybe you could try something like this (untested, really just a quick hack):

(defmethod size ((object standard-object))
  (let ((size (ccl:object-direct-size object)))
    (dolist (slot (mapcar #'ccl:slot-definition-name
                          (ccl:class-slots (class-of object))))
      (when (slot-boundp object slot)
        (incf size (size (slot-value object slot)))))
    size))

(defmethod size ((list list))
  (reduce (lambda (acc object) (+ acc (size object)))
          list
          :initial-value (ccl:object-direct-size list)))

(defmethod size (object)
  (ccl:object-direct-size object))

例如:

CL-USER> (defclass foo ()
           ((child :accessor child :initarg :child)))
#<STANDARD-CLASS FOO>
CL-USER> (defclass bar (foo)
           ((child2 :accessor child2 :initarg :child2)))
#<STANDARD-CLASS BAR>
CL-USER> (size '())
0
CL-USER> (size "foo")
16
CL-USER> (size '("foo" "bar"))
40
CL-USER> (size (make-instance 'foo))
16
CL-USER> (size (make-instance 'foo :child '("foo" "bar" "baz")))
72
CL-USER> (size (make-instance
                'bar
                :child "foo"
                :child2 (make-instance 'foo :child (make-array 100))))
456

这篇关于普通Lisp中对象的内存使用情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆