调用ObjectSpace.count_objects时哈希键的含义是什么?

时间:2021-09-28 15:42:46

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

在Ruby 1.9(YARV)中,您可以获得所有当前分配的对象的计数,如下所示:

ObjectSpace.count_objects

which returns a hash like

它返回一个哈希值

{:TOTAL=>1226560, :FREE=>244204, :T_OBJECT=>26141, :T_CLASS=>9819, :T_MODULE=>1420, :T_FLOAT=>287,
 :T_STRING=>260476, :T_REGEXP=>4081, :T_ARRAY=>72269, :T_HASH=>14923, :T_STRUCT=>4601, :T_BIGNUM=>7,
 :T_FILE=>16, :T_DATA=>54553, :T_MATCH=>5, :T_COMPLEX=>1, :T_RATIONAL=>15, :T_NODE=>524818, 
 :T_ICLASS=>8924}

What is the meaning of these hash keys? Some like T_STRING and T_FILE are obvious. I'm particularly curious about :FREE, :T_ICLASS, :T_DATA, and :T_NODE.

这些哈希键的含义是什么?有些像T_STRING和T_FILE是显而易见的。我特别好奇:FREE,:T_ICLASS,:T_DATA,和:T_NODE。

3 个解决方案

#1


4  

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

只是一个猜测:我假设:T_ICLASS计数包括类和:T_NODE可能代表AST节点。

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:

RUBY_T​​_xyz枚举的定义如下:

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.

我认为其中大多数都很明显。我唯一想不到的是T_DATA(参见@ banister的评论),T_ZOMBIE和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:

顺便说一句:请注意,这些不是Ruby 1.9的一部分。他们是YARV的一部分。它们在Ruby 1.9的其他实现上可能完全不同,甚至根本不存在。文件明确指出:

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.

除了C Ruby之外,预计此方法不起作用。

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

(作者可能认为该方法仅保证在MRI和YARV上工作。)

#2


1  

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

您可以通过调用ObjectSpace.count_tdata_objects(此处描述)来获取有关T_DATA类别的更多信息。

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

我相信这些是由VM控制的本机对象。有时,原生扩展也可以分配它们。

#3


0  

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

这些类型在Ruby源代码中的doc / extension.doc文件中描述:

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

#1


4  

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

只是一个猜测:我假设:T_ICLASS计数包括类和:T_NODE可能代表AST节点。

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:

RUBY_T​​_xyz枚举的定义如下:

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.

我认为其中大多数都很明显。我唯一想不到的是T_DATA(参见@ banister的评论),T_ZOMBIE和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:

顺便说一句:请注意,这些不是Ruby 1.9的一部分。他们是YARV的一部分。它们在Ruby 1.9的其他实现上可能完全不同,甚至根本不存在。文件明确指出:

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.

除了C Ruby之外,预计此方法不起作用。

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

(作者可能认为该方法仅保证在MRI和YARV上工作。)

#2


1  

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

您可以通过调用ObjectSpace.count_tdata_objects(此处描述)来获取有关T_DATA类别的更多信息。

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

我相信这些是由VM控制的本机对象。有时,原生扩展也可以分配它们。

#3


0  

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

这些类型在Ruby源代码中的doc / extension.doc文件中描述:

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