8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

Ruby | ObjectSpace で Heap 内にある全てのオブジェクトを取得し、Classごとのインスタンス数を一覧化する

Posted at

Ruby | ObjectSpace で Heap 内にある全てのオブジェクトを取得し、Classごとのインスタンス数を一覧化する

概要

ObjectSpace で Heap 内にある全てのオブジェクトを取得し、Classごとのインスタンス数を一覧化します。

ObjectSpace については下記参照。
http://docs.ruby-lang.org/ja/2.1.0/class/ObjectSpace.html#M_EACH_OBJECT

コード

require 'tbpgr_utils'

print ObjectSpace
    .each_object
    .group_by { |e|e.class }
    .map { |k, v|{k => v.size} }
    .sort_by { |k, v|k.to_s }
    .to_table

__END__
to_table は自作 gem tbpgr_utils のメソッドです。
配列から、マークダウンなどで利用されるパイプ区切りのテーブル文字列を返却します。

出力

|                           ARGF.class|    1|
|ActiveSupport::Inflector::Inflections|    1|
| ActiveSupport::Notifications::Fanout|    1|
|                                Array| 4845|
|                               Bignum|   16|
|                              Binding|    1|
|                                Class|  381|
|                              Complex|    1|
|                                 Data|    1|
|                             Encoding|  100|
|                           Enumerator|    1|
|                                 File|   47|
|                                Float|    9|
|                      Gem::Dependency|  410|
|                     Gem::PathSupport|    1|
|                        Gem::Platform|    5|
|                     Gem::Requirement|  613|
|                   Gem::Specification|  102|
|     Gem::StubSpecification::StubLine|   18|
|               Gem::StubSpecification|  119|
|                         Gem::Version|  189|
|                                 Hash|  206|
|                         I18n::Config|    1|
|                                   IO|    3|
|                              IOError|    1|
|                            LoadError|    1|
|                            MatchData|    8|
|                               Module|   68|
|                              Monitor|    1|
|                                Mutex|    7|
|                   NameError::message|   60|
|                        NoMemoryError|    1|
|                        NoMethodError|   60|
|                               Object|    3|
|                 OpenSSL::X509::Store|    1|
|                                Prime|    1|
|                                 Proc|  175|
|                               Random|    1|
|                                Range|    9|
|                               Regexp|  248|
|                          RubyVM::Env|  154|
|          RubyVM::InstructionSequence| 1832|
|                               RubyVM|    1|
|                               String|27029|
|                     SystemStackError|    1|
|                    Thread::Backtrace|   76|
|                               Thread|    1|
|                          ThreadGroup|    1|
|                    ThreadSafe::Cache|    2|
|                                 Time|   99|
|                                fatal|    1|

出力内容をMarkdownのテーブルにしてみる

Class instances
ARGF.class 1
ActiveSupport::Inflector::Inflections 1
ActiveSupport::Notifications::Fanout 1
Array 4845
Bignum 16
Binding 1
Class 381
Complex 1
Data 1
Encoding 100
Enumerator 1
File 47
Float 9
Gem::Dependency 410
Gem::PathSupport 1
Gem::Platform 5
Gem::Requirement 613
Gem::Specification 102
Gem::StubSpecification::StubLine 18
Gem::StubSpecification 119
Gem::Version 189
Hash 206
I18n::Config 1
IO 3
IOError 1
LoadError 1
MatchData 8
Module 68
Monitor 1
Mutex 7
NameError::message 60
NoMemoryError 1
NoMethodError 60
Object 3
OpenSSL::X509::Store 1
Prime 1
Proc 175
Random 1
Range 9
Regexp 248
RubyVM::Env 154
RubyVM::InstructionSequence 1832
RubyVM 1
String 27029
SystemStackError 1
Thread::Backtrace 76
Thread 1
ThreadGroup 1
ThreadSafe::Cache 2
Time 99
fatal 1
8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?