简体   繁体   English

如果我使用的是 Heroku buildpack,是否需要使用 jemalloc 构建 Ruby 并使用 gem?

[英]Do I need to build Ruby with jemalloc and use the gem if I'm using a Heroku buildpack?

Sample a Ruby (2.4.1) on Rails (4.2.8) app, configured with Puma and deployed on Heroku (cedar-16) .示例Ruby (2.4.1) on Rails (4.2.8)应用程序,使用 Puma 配置并部署在Heroku (cedar-16)上。 Currently developing on a Mojave 10.14.5 MBP.目前在Mojave 10.14.5 MBP 上开发。 I'm on a mission to cut down memory usage and recently discovered jemalloc via this post .我的任务是减少 memory 的使用,最近通过这篇文章发现了 jemalloc。 I reinstalled Ruby (using RVM) with jemalloc enabled as proved by the following output - -lpthread -ljemalloc -lgmp -ldl -lobjc .我重新安装了 Ruby(使用 RVM)并启用了 jemalloc,如以下 output - -lpthread -ljemalloc -lgmp -ldl -lobjc所证明的那样。 I proceeded to add the jemalloc gem and bundle installed without a problem.我继续添加jemalloc gem和 bundle 安装没有问题。 I've also set up the Heroku buildpack and configured necessary environment variables.我还设置了Heroku buildpack并配置了必要的环境变量。

However, with the gem in my Gemfile, I'm unable to run any rake/rails commands - rails c returns the following error, promptly followed by my Mac's fans firing up:但是,使用我的 Gemfile 中的 gem,我无法运行任何 rake/rails 命令 - rails c返回以下错误,然后我的 Mac 的粉丝立即启动:

ruby(45487,0x10739c5c0) malloc: *** malloc_zone_unregister() failed for 0x7fff94a3f000

foreman start gets stuck at * Preloading application . foreman start卡在* Preloading application All issues vanish & app works fine if I remove the gem.如果我删除 gem,所有问题都会消失并且应用程序可以正常工作。

Since I'm not privy to how Heroku buildpacks work and how that specific one is programmed exactly, my questions are as follows:由于我不了解 Heroku 构建包的工作方式以及该特定构建包的精确编程方式,因此我的问题如下:

  1. With the buildpack added, do I really need the gem installed in my project?添加 buildpack 后,我真的需要在项目中安装 gem 吗?
  2. With the buildpack added, do I need a Ruby version compiled using jemalloc?添加 buildpack 后,是否需要使用 jemalloc 编译的 Ruby 版本?
  3. If I do need the gem, how do I get rid of the aforementioned error?如果我确实需要 gem,我该如何摆脱上述错误?

malloc gem was last released 5 years ago and it bundles a pretty old version of jemalloc lib (v3.4 and currently we have v5.2). malloc gem 上一次发布是在 5 年前,它捆绑了一个相当旧版本的 jemalloc lib(v3.4,目前我们有 v5.2)。 All the gem does, is adding jemalloc lib to LD_PRELOAD (or DYLD_INSERT_LIBRARIES on mac os) before executing ruby binary. gem 所做的就是在执行 ruby 二进制文件之前将 jemalloc lib 添加到LD_PRELOAD (或 mac os 上的DYLD_INSERT_LIBRARIES )。 It shouldn't be needed if ruby is built against jemalloc library with --with-jemalloc flag.如果 ruby 是针对带有--with-jemalloc标志的 jemalloc 库构建的,则不需要它。

So to answer your questions:所以回答你的问题:

  1. With the buildpack added, do I really need the gem installed in my project?添加 buildpack 后,我真的需要在项目中安装 gem 吗?

No, buildpack will handle everything automatically for you while deploying to heroku.不,buildpack 会在部署到 heroku 时自动为您处理一切。 Actually, there might be a problem with conflicting jemalloc versions if you have the gem.实际上,如果你有 gem,jemalloc 版本冲突可能会出现问题。

  1. With the buildpack added, do I need a Ruby version compiled using jemalloc?添加 buildpack 后,是否需要使用 jemalloc 编译的 Ruby 版本?

For local development?为当地发展? Technically no.技术上没有。 But I would advise you to use exactly the same ruby version as you use on production to avoid suprises.但我建议您使用与生产中使用的完全相同的 ruby 版本以避免意外。

  1. If I do need the gem, how do I get rid of the aforementioned error?如果我确实需要 gem,我该如何摆脱上述错误?

You don't need the gem.你不需要宝石。 You can enable jemalloc by rebuilding your ruby with --with-jemalloc flag (as you did).您可以通过使用--with-jemalloc标志(如您所做的那样)重建 ruby 来启用 jemalloc。

Here's a full list of steps:以下是完整的步骤列表:

# install jemalloc lib locally
brew install jemalloc

# reinstall ruby (with rbenv):
RUBY_CONFIGURE_OPTS=--with-jemalloc rbenv install 2.7.1
# or with rvm:
rvm reinstall 2.7.1 -C —with-jemalloc

# confirm ruby is using jemalloc (ruby >= 2.6)
ruby -r rbconfig -e "puts RbConfig::CONFIG['MAINLIBS']"
# or with ruby < 2.6
ruby -r rbconfig -e "puts RbConfig::CONFIG['LIBS']"

You might also need to rebuild gems with native extensions (though I'm not sure about this one): See this question for how to do it: How do I get a list of gems that are installed that have native extensions?您可能还需要使用本机扩展重建 gem(尽管我不确定这个):请参阅此问题以了解如何执行此操作: 如何获取已安装的具有本机扩展的 gem 列表?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM