在后台进程中排除Heroku R14 & R15内存错误

oxalkeyp  于 2022-11-13  发布在  其他
关注(0)|答案(2)|浏览(132)

我正在寻找一些R14和R15内存错误的故障排除帮助,我经常在Heroku上运行的Rails应用程序的日志中发现这些错误。

Jan 15 08:34:12 myapp-production app/worker.1:  2018-1-15T16:34:11+0000: [Worker(host:a833f658-b5 pid:4)] Job StoryAssetMailer#perform (id=7776) RUNNING 
Jan 15 08:36:31 myapp-production heroku/worker.1: Process running mem=518M(100.0%) 
Jan 15 08:36:31 myapp-production heroku/worker.1: Error R14 (Memory quota exceeded) 
Jan 15 08:36:33 myapp-production heroku/worker.1: Error R15 (Memory quota vastly exceeded)

我已经按照Heroku的R14支持页面上的建议设置了WEB_CONCURRENCY=1。这没有什么区别。
问题是我真的不知道用什么工具来测量内存峰值(泄漏?)发生在哪里?我可以找到违规的方法(包括在下面),但寻找如何缩小问题的确切位置的想法,然后我会担心如何修复它。
后台进程创建错误为Job StoryAssetMailer#perform

# app/jobs/story_asset_mailer.rb
require Rails.root.join('lib', 's3_store')
require 'zip'

class StoryAssetMailer
  def initialize(recipient:, story:)
    @recipient = recipient
    @story = story
  end

  def perform
    stored_file = S3Store.new(
      zip_file_stream,
      @story.download_safe_title(
        extension: 'zip'
      )
    ).store(method: :sysread)

    Mailer.story_images(
      recipient: @recipient,
      story: @story,
      file_url: stored_file.url
    ).deliver
  end

  def zip_file_stream
    manifest = ZipManifest.build(media_files_for_story)
    build_zip_file(manifest)
  end   
  ...
end

相关型号:

#app/models/zip_manifest.rb
require 'open-uri'

class ZipManifest
  def initialize(manifest: nil)
    @manifest ||= manifest
  end

  def each_file
    @manifest.each do |contents, filename|
      yield filename, contents
    end
  end

  def self.build(images)
    new(
      manifest:
        images.map do |image|
          [open(image.url), image.filename]
        end
    )
  end
end

编辑:这东西开着吗?

vd8tlhqk

vd8tlhqk1#

请仔细检查您的文件。因为RAM也会为您的文件分配内存。如果您的文件是500M,那么总内存应该是(500M +后台进程的内存)。

5jvtdoz2

5jvtdoz22#

你可以使用https://devcenter.heroku.com/articles/log-runtime-metrics heroku插件来真实的观察你的内存消耗。
你的进程内存使用直接取决于你正在处理的文件的大小,而你正在使用最小的dyno(512 Mb)。你可能想扩大它。
此外,WEB_CONCURRENCY通常用于控制Web动态并发,而不是工作线程。
R15错误(大大超出配额)意味着您的配额已超过200%(https://devcenter.heroku.com/articles/error-codes#r15-memory-quota-vastly-exceeded),这会导致heroku终止进程。
R14错误(超出内存配额)是一个警告,当你超过100%但低于200%的内存配额。它只影响性能(测功器开始交换)。
因此,请在应用中设置最大资产文件大小限制,并确保asset file size + your app slug size + some safety margin在dyno内存限制范围内。

相关问题