1

私は最近、新しいObjectSpace.dump_allメソッドを使用して、一連の Ruby 2.1.2 コードをプロファイリングしました。いくつかの有用なデータが返されました (@tmm1 がそのリンクで提案しているスクリプトを使用):

 931 /app/vendor/bundle/ruby/2.1.0/gems/polyglot-0.3.5/lib/polyglot.rb:65:ARRAY
1015 /app/vendor/bundle/ruby/2.1.0/gems/activemodel-4.1.5/lib/active_model/attribute_methods.rb:385:ARRAY
1015 /app/vendor/bundle/ruby/2.1.0/gems/activemodel-4.1.5/lib/active_model/attribute_methods.rb:385:DATA
1015 /app/vendor/bundle/ruby/2.1.0/gems/activemodel-4.1.5/lib/active_model/attribute_methods.rb:385:NODE
1054 ::REGEXP
1075 ::ICLASS
1095 /app/vendor/ruby-2.1.2/lib/ruby/2.1.0/psych/class_loader.rb:32:STRING
1753 /app/vendor/cache/aws-sdk-core-ruby-99f5012f1162/aws-sdk-core/lib/seahorse/client/net_http/connection_pool.rb:49:ARRAY
1753 /app/vendor/cache/aws-sdk-core-ruby-99f5012f1162/aws-sdk-core/lib/seahorse/client/net_http/connection_pool.rb:49:STRING
1969 /app/vendor/bundle/ruby/2.1.0/gems/polyglot-0.3.5/lib/polyglot.rb:65:NODE
2036 /app/vendor/bundle/ruby/2.1.0/gems/polyglot-0.3.5/lib/polyglot.rb:65:DATA
2507 /app/vendor/bundle/ruby/2.1.0/gems/pg-0.17.1/lib/pg/result.rb:10:STRING
3227 /app/vendor/bundle/ruby/2.1.0/gems/polyglot-0.3.5/lib/polyglot.rb:65:STRING
4592 ::OBJECT
5291 ::CLASS
15623 ::NODE
19227 ::ARRAY
25977 ::DATA
27162 ::HASH
140490 ::STRING

私の質問は、ファイル名や行番号 (最後の 6 行) が見えない場所に、なぜそんなに多くのオブジェクトが割り当てられているのでしょうか? 私の問題は、ぶらぶらしている多くの文字列に関係しているようですが、このアプリの機能を考えると、これは間違いなく理にかなっています。しかし、これらの文字列がどこから構築されているのかわからないということは、何もできないことを意味します。

ObjectSpace.trace_object_allocations_startプロファイリングしようとしている操作のかなり前に、アプリの起動時に実行しています。そして、私は間違いなくGC.startの各呼び出しの前に呼び出していますObjectSpace.dump_all@krasnoukhov の投稿.dump_allと同様の方法で 100 ジョブごとにダンプする Sidekiq ミドルウェアを呼び出しています。

私の本当の質問は、Ruby オブジェクトの割り当てを十分に理解している人が、なぜObjectSpaceそれらの起源を知らないのかを説明できる人がいるSTRINGsかどうかということだと思います。

ありがとう!

編集: @Krasnoukhov のブログとほぼ同じコードを使用しています。

if ENV["PROFILE"]
  require "objspace"
  ObjectSpace.trace_object_allocations_start
  Sidekiq.logger.info "allocations tracing enabled"

  module Sidekiq
    module Middleware
      module Server
        class Profiler
          # Number of jobs to process before reporting
          JOBS = 100
      class << self
        mattr_accessor :counter
        self.counter = 0

        def synchronize(&block)
          @lock ||= Mutex.new
          @lock.synchronize(&block)
        end
      end

      def call(worker_instance, item, queue)
        begin
          yield
        ensure
          self.class.synchronize do
            self.class.counter += 1

            if self.class.counter % JOBS == 0
              Sidekiq.logger.info "reporting allocations after #{self.class.counter} jobs"
              GC.start
              ObjectSpace.dump_all(output: File.open("heap.json", "w"))
              Sidekiq.logger.info "heap saved to heap.json"
            end
          end
        end
      end
    end
  end
end


Sidekiq.configure_server do |config|
  config.server_middleware do |chain|
      chain.add Sidekiq::Middleware::Server::Profiler
    end
  end
end
4

0 に答える 0