├── .gitignore ├── ChangeLog ├── Gemfile ├── Gemfile.lock ├── README.md ├── Rakefile ├── bin └── je ├── ext └── jemalloc │ ├── extconf.rb │ └── jemalloc-3.4.0.tar.bz2 ├── jemalloc.gemspec └── lib └── jemalloc └── version.rb /.gitignore: -------------------------------------------------------------------------------- 1 | *.o 2 | *.so 3 | *~ 4 | ext/jemalloc/*.c 5 | ext/jemalloc/jemalloc-3.0.0/ 6 | ruby/Makefile 7 | *.5 8 | *.8 9 | *.6 10 | _obj 11 | _test 12 | -------------------------------------------------------------------------------- /ChangeLog: -------------------------------------------------------------------------------- 1 | Release 1.0.1 - 2015/01/15 2 | - just exec without spawn 3 | 4 | Release 1.0.0 - 2013/09/23 5 | - jemalloc v3.4.0 6 | - use longer timeout for SIGTERM 7 | 8 | Release 0.1.9 - 2013/03/09 9 | - jemalloc v3.3.1 10 | 11 | Release 0.1.8 - 2013/01/29 12 | - jemalloc v3.3.0 13 | 14 | Release 0.1.7 - 2012/11/22 15 | - fixed build for ruby 2.0.0-preview1 16 | 17 | Release 0.1.6 - 2012/11/09 18 | - jemalloc v3.2.0 19 | 20 | Release 0.1.5 - 2012/10/21 21 | - fixed homepage at gemspec 22 | 23 | Release 0.1.4 - 2012/10/21 24 | - change gem name from je to jemalloc 25 | 26 | Release 0.1.3 - 2012/10/16 27 | - jemalloc v3.1.0 28 | 29 | Release 0.1.2 - 2012/09/19 30 | - support -v option 31 | - jemalloc v3.0.0 32 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | source 'https://rubygems.org' 2 | gemspec 3 | -------------------------------------------------------------------------------- /Gemfile.lock: -------------------------------------------------------------------------------- 1 | PATH 2 | remote: . 3 | specs: 4 | jemalloc (1.0.1) 5 | 6 | GEM 7 | remote: https://rubygems.org/ 8 | specs: 9 | rake (0.9.2.2) 10 | rake-compiler (0.7.9) 11 | rake 12 | 13 | PLATFORMS 14 | ruby 15 | 16 | DEPENDENCIES 17 | bundler (>= 1.0.0) 18 | jemalloc! 19 | rake (>= 0.8.7) 20 | rake-compiler (~> 0.7.1) 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # jemalloc 2 | 3 | Instant [jemalloc](http://www.canonware.com/jemalloc/) injection into Ruby apps, for better performance and less memory. 4 | 5 | # Why jemalloc? 6 | 7 | Ruby relies on malloc(3) for its internal memory allocation. Using better malloc() implementation will boost your application performance, and supress the memory usage. 8 | 9 | jemalloc is a malloc(3) implementation, originally developed by Jason Evans. jemalloc handles small object better than other allocators so usually gives better performance and memory usage to Ruby programs. 10 | 11 | # Why jemalloc? 12 | 13 | Installing jemalloc separately from Ruby is pain in some cases (e.g. Heroku, EngineYard, etc). `je` gem contains jemalloc itself within a gem, and enables instant jemalloc injection in a really easy way: install `je` gem, and launch your app with `je` command. 14 | 15 | # Install 16 | 17 | Install `jemalloc` gem in your application. For [bundler](http://gembundler.com/) based application, please add the following line into your Gemfile, and and install `jemalloc` by `bundle install`. 18 | 19 | gem 'jemalloc' 20 | 21 | # Usage 22 | 23 | Execute your application with `je` command, which is contained in `je` gem. Example command for Rails + bundler application is like follows. 24 | 25 | $ bundle exec je ./script/rails s 26 | 27 | `-v` option will let you confirm jemalloc is actually injected. 28 | 29 | $ bundle exec je -v ./script/rails s 30 | => Injecting jemalloc... 31 | => Booting WEBrick 32 | ... 33 | 34 | # Limitation 35 | 36 | Currently, this gem works only on Linux and Mac OS X. 37 | 38 | # License 39 | 40 | [BSD-derived License](http://www.canonware.com/jemalloc/license.html). 41 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | # -*- mode: ruby; coding: utf-8 -*- 2 | require 'rubygems' 3 | require 'bundler' 4 | 5 | require 'rake' 6 | require 'rake/clean' 7 | require 'rake/testtask' 8 | require 'rubygems/package_task' 9 | 10 | Bundler::GemHelper.install_tasks 11 | 12 | begin 13 | require 'rake/extensiontask' 14 | Rake::ExtensionTask.new('jemalloc') 15 | rescue LoadError 16 | abort "This Rakefile requires rake-compiler (gem install rake-compiler)" 17 | end 18 | -------------------------------------------------------------------------------- /bin/je: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env ruby 2 | # Help 3 | def usage 4 | puts < Injecting jemalloc..." if $verbose 32 | ENV.store("LD_PRELOAD", lib_dir + "/jemalloc.so") 33 | elsif File.exists? (lib_dir + "/jemalloc.bundle") 34 | puts "=> Injecting jemalloc..." if $verbose 35 | ENV.store("DYLD_INSERT_LIBRARIES", lib_dir + "/jemalloc.bundle") 36 | else 37 | puts "=> Skip injecting jemalloc. Your platform is not supported." if $verbose 38 | end 39 | 40 | Kernel.exec *argv 41 | -------------------------------------------------------------------------------- /ext/jemalloc/extconf.rb: -------------------------------------------------------------------------------- 1 | require 'mkmf' 2 | require 'rbconfig' 3 | 4 | $stdout.sync = true 5 | pkg = "jemalloc-3.4.0" 6 | 7 | def sys(cmd) 8 | puts "$ #{cmd}" 9 | unless ret = xsystem(cmd) 10 | raise "system command `#{cmd}' failed, please report to https://github.com/ibc/em-udns/issues" 11 | end 12 | ret 13 | end 14 | 15 | # monfigure and copy sources to cur_dir 16 | src_dir = File.expand_path(File.dirname(__FILE__)) 17 | cur_dir = Dir.pwd 18 | Dir.chdir File.dirname(__FILE__) do 19 | # cleanup 20 | FileUtils.remove_dir(pkg, force = true) 21 | 22 | # decompress and copy source files 23 | sys "tar vjxf #{pkg}.tar.bz2" 24 | Dir.chdir(pkg) do 25 | # configure 26 | sys "./configure" 27 | # zone.c is only for Mac OS X 28 | if RbConfig::CONFIG['target_vendor'] != "apple" 29 | sys "rm -fR src/zone.c" 30 | end 31 | # mkmf only allows *.c files on current dir 32 | sys "cp src/*.c #{src_dir}" 33 | end 34 | end 35 | Dir.chdir cur_dir 36 | 37 | include_dir= File.dirname(__FILE__) + "/#{pkg}/include/" 38 | $CFLAGS << %[ -std=gnu99 -Wall -pipe -g3 -fvisibility=hidden -O3 -funroll-loops -D_GNU_SOURCE -D_REENTRANT -I.. -I#{include_dir}] 39 | 40 | create_makefile('jemalloc') 41 | 42 | # Modify Makefile to create .dylib, instead of .bundle. Because extconf.rb 43 | # generates .bundle by default, but .bundle cannot be dynamically loaded by 44 | # LD_PRELOAD. 45 | # NOTICE: Mac OS X only 46 | if RbConfig::CONFIG['target_vendor'] == "apple" 47 | makefile = open('Makefile').read 48 | # for 1.9.2 and 1.9.3 49 | if makefile =~ /-dynamic\ -bundle/ && makefile =~ /-flat_namespace/ 50 | makefile.gsub!(/-dynamic\ -bundle/, '-shared') 51 | makefile.gsub!(/-flat_namespace/, '-dylib_install_name') 52 | # for 2.0.0 53 | elsif makefile =~ /-dynamic\ -bundle/ 54 | makefile.gsub!(/-dynamic\ -bundle/, '-shared') 55 | else 56 | raise 'Your platform is not supported. Please report to http://github.com/treasure-data/jemalloc-rb' 57 | end 58 | open('Makefile', 'w'){ |f| f.write(makefile) } 59 | end 60 | -------------------------------------------------------------------------------- /ext/jemalloc/jemalloc-3.4.0.tar.bz2: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/kzk/jemalloc-rb/864fceee6b0185b9914609ebd7bd7d412fd31d43/ext/jemalloc/jemalloc-3.4.0.tar.bz2 -------------------------------------------------------------------------------- /jemalloc.gemspec: -------------------------------------------------------------------------------- 1 | # -*- mode: ruby; coding: utf-8 -*- 2 | $:.push File.expand_path("../lib", __FILE__) 3 | require 'jemalloc/version' 4 | 5 | Gem::Specification.new do |s| 6 | s.name = "jemalloc" 7 | s.version = JEMalloc::VERSION 8 | s.summary = "use jemalloc as default allocator, everywhere!" 9 | s.description = %q{Use jemalloc as default allocator, everywhere!} 10 | s.author = "Kazuki Ohta" 11 | s.email = "kazuki.ohta@gmail.com" 12 | s.homepage = "https://github.com/treasure-data/jemalloc-rb" 13 | s.extensions = ["ext/jemalloc/extconf.rb"] 14 | 15 | s.files = `git ls-files`.split("\n") 16 | s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n") 17 | s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) } 18 | s.require_paths = ["lib"] 19 | 20 | s.add_development_dependency 'bundler', ['>= 1.0.0'] 21 | s.add_development_dependency 'rake', ['>= 0.8.7'] 22 | s.add_development_dependency 'rake-compiler', ['~> 0.7.1'] 23 | end 24 | -------------------------------------------------------------------------------- /lib/jemalloc/version.rb: -------------------------------------------------------------------------------- 1 | module JEMalloc 2 | VERSION = "1.0.1" 3 | end 4 | --------------------------------------------------------------------------------