What are the meanings of the hash keys when calling ObjectSpace.count_objects?

前端 未结 3 1182
说谎
说谎 2021-01-12 07:18

In Ruby 1.9 (YARV) you can get a count of all currently allocated objects like so:

ObjectSpace.count_objects

which returns a hash like

相关标签:
3条回答
  • 2021-01-12 07:59

    Just a guess: I assume :T_ICLASS counts include classes and :T_NODE could maybe stand for AST nodes.

    Here's a full list (unfortunately without comments):

    #define T_NONE   RUBY_T_NONE
    #define T_NIL    RUBY_T_NIL
    #define T_OBJECT RUBY_T_OBJECT
    #define T_CLASS  RUBY_T_CLASS
    #define T_ICLASS RUBY_T_ICLASS
    #define T_MODULE RUBY_T_MODULE
    #define T_FLOAT  RUBY_T_FLOAT
    #define T_STRING RUBY_T_STRING
    #define T_REGEXP RUBY_T_REGEXP
    #define T_ARRAY  RUBY_T_ARRAY
    #define T_HASH   RUBY_T_HASH
    #define T_STRUCT RUBY_T_STRUCT
    #define T_BIGNUM RUBY_T_BIGNUM
    #define T_FILE   RUBY_T_FILE
    #define T_FIXNUM RUBY_T_FIXNUM
    #define T_TRUE   RUBY_T_TRUE
    #define T_FALSE  RUBY_T_FALSE
    #define T_DATA   RUBY_T_DATA
    #define T_MATCH  RUBY_T_MATCH
    #define T_SYMBOL RUBY_T_SYMBOL
    #define T_RATIONAL RUBY_T_RATIONAL
    #define T_COMPLEX RUBY_T_COMPLEX
    #define T_UNDEF  RUBY_T_UNDEF
    #define T_NODE   RUBY_T_NODE
    #define T_ZOMBIE RUBY_T_ZOMBIE
    #define T_MASK   RUBY_T_MASK
    

    The RUBY_T_xyz enum is defined like this:

    enum ruby_value_type {
        RUBY_T_NONE   = 0x00,
    
        RUBY_T_OBJECT = 0x01,
        RUBY_T_CLASS  = 0x02,
        RUBY_T_MODULE = 0x03,
        RUBY_T_FLOAT  = 0x04,
        RUBY_T_STRING = 0x05,
        RUBY_T_REGEXP = 0x06,
        RUBY_T_ARRAY  = 0x07,
        RUBY_T_HASH   = 0x08,
        RUBY_T_STRUCT = 0x09,
        RUBY_T_BIGNUM = 0x0a,
        RUBY_T_FILE   = 0x0b,
        RUBY_T_DATA   = 0x0c,
        RUBY_T_MATCH  = 0x0d,
        RUBY_T_COMPLEX  = 0x0e,
        RUBY_T_RATIONAL = 0x0f,
    
        RUBY_T_NIL    = 0x11,
        RUBY_T_TRUE   = 0x12,
        RUBY_T_FALSE  = 0x13,
        RUBY_T_SYMBOL = 0x14,
        RUBY_T_FIXNUM = 0x15,
    
        RUBY_T_UNDEF  = 0x1b,
        RUBY_T_NODE   = 0x1c,
        RUBY_T_ICLASS = 0x1d,
        RUBY_T_ZOMBIE = 0x1e,
    
        RUBY_T_MASK   = 0x1f
    };
    

    I think most of those are rather obvious. The only ones I can't figure out are T_DATA (see @banister's comment), T_ZOMBIE and T_MASK.

    BTW: Note that these are not part of Ruby 1.9. They are part of YARV. They might be totally different on other implementations of Ruby 1.9 or even not exist at all. The documentation clearly states:

    The contents of the returned hash is implementation defined. It may be changed in future.

    In fact, it isn't even guaranteed that the method itself exists:

    This method is not expected to work except C Ruby.

    (By which the author presumably means that the method is only guaranteed to work on MRI and YARV.)

    0 讨论(0)
  • 2021-01-12 07:59

    The types are described in the file doc/extension.doc in Ruby source code:

    T_NIL       :: nil
    T_OBJECT    :: ordinary object
    T_CLASS     :: class
    T_MODULE    :: module
    T_FLOAT     :: floating point number
    T_STRING    :: string
    T_REGEXP    :: regular expression
    T_ARRAY     :: array
    T_HASH      :: associative array
    T_STRUCT    :: (Ruby) structure
    T_BIGNUM    :: multi precision integer
    T_FIXNUM    :: Fixnum(31bit or 63bit integer)
    T_COMPLEX   :: complex number
    T_RATIONAL  :: rational number
    T_FILE      :: IO
    T_TRUE      :: true
    T_FALSE     :: false
    T_DATA      :: data
    T_SYMBOL    :: symbol
    

    In addition, there are several other types used internally:

    T_ICLASS    :: included module
    T_MATCH     :: MatchData object
    T_UNDEF     :: undefined
    T_NODE      :: syntax tree node
    T_ZOMBIE    :: object awaiting finalization
    
    0 讨论(0)
  • 2021-01-12 08:12

    You can get more information about the T_DATA category by calling ObjectSpace.count_tdata_objects (described here).

    I believe that these are native objects controlled by the VM. Sometimes native extensions can allocate them, as well.

    0 讨论(0)
提交回复
热议问题