Skip to content

Instantly share code, notes, and snippets.

@sandersch
Last active December 14, 2015 10:49
Show Gist options
  • Save sandersch/5074553 to your computer and use it in GitHub Desktop.
Save sandersch/5074553 to your computer and use it in GitHub Desktop.
Test Ruby's ability to release memory back to system. Results on Ubuntu 12.04 amd64 using rvm binary rubies.
Ruby 1.9.3-p392
==================
All memory used before 1st run - 7 MB
Memory consumed by 1st run of eat_up_memory - 440 MB
All memory used before 2nd run - 447 MB
Memory consumed by 2nd run of eat_up_memory - 5 MB
All memory used before 3rd run - 453 MB
Memory consumed by 3rd run of eat_up_memory - 9 MB
Memory used before explicit GC.start - 463 MB
Memory used after explicit GC.start - 463 MB
Ruby 2.0.0-p0
=============
All memory used before 1st run - 7 MB
Memory consumed by 1st run of eat_up_memory - 437 MB
All memory used before 2nd run - 445 MB
Memory consumed by 2nd run of eat_up_memory - 49 MB
All memory used before 3rd run - 495 MB
Memory consumed by 3rd run of eat_up_memory - 16 MB
Memory used before explicit GC.start - 511 MB
Memory used after explicit GC.start - 511 MB
Ruby 2.0.0-p0 RUBY_FREE_MIN=200000 RUBY_GC_MALLOC_LIMIT=60000000
================================================================
All memory used before 1st run - 8 MB
Memory consumed by 1st run of eat_up_memory - 485 MB
All memory used before 2nd run - 493 MB
Memory consumed by 2nd run of eat_up_memory - 8 MB
All memory used before 3rd run - 501 MB
Memory consumed by 3rd run of eat_up_memory - 0 MB
Memory used before explicit GC.start - 501 MB
Memory used after explicit GC.start - 501 MB
Using /home/charlie/.rvm/gems/ruby-1.9.3-p392
All memory used before 1st run - 7 MB
Memory consumed by 1st run of eat_up_memory - 439 MB
All memory used before 2nd run - 446 MB
Memory consumed by 2nd run of eat_up_memory - 4 MB
All memory used before 3rd run - 450 MB
Memory consumed by 3rd run of eat_up_memory - 14 MB
Memory used before explicit GC.start - 464 MB
Memory used after explicit GC.start - 464 MB
Using /home/charlie/.rvm/gems/ruby-2.0.0-p0
All memory used before 1st run - 7 MB
Memory consumed by 1st run of eat_up_memory - 438 MB
All memory used before 2nd run - 447 MB
Memory consumed by 2nd run of eat_up_memory - 58 MB
All memory used before 3rd run - 505 MB
Memory consumed by 3rd run of eat_up_memory - 11 MB
Memory used before explicit GC.start - 517 MB
Memory used after explicit GC.start - 517 MB
Using /home/charlie/.rvm/gems/ruby-1.9.3-p392
All memory used before 1st run - 7 MB
Memory consumed by 1st run of eat_up_memory - 439 MB
All memory used before 2nd run - 446 MB
Memory consumed by 2nd run of eat_up_memory - 4 MB
All memory used before 3rd run - 451 MB
Memory consumed by 3rd run of eat_up_memory - 4 MB
Memory used before explicit GC.start - 455 MB
Memory used after explicit GC.start - 455 MB
Using /home/charlie/.rvm/gems/ruby-2.0.0-p0
All memory used before 1st run - 7 MB
Memory consumed by 1st run of eat_up_memory - 439 MB
All memory used before 2nd run - 447 MB
Memory consumed by 2nd run of eat_up_memory - 54 MB
All memory used before 3rd run - 502 MB
Memory consumed by 3rd run of eat_up_memory - 11 MB
Memory used before explicit GC.start - 514 MB
Memory used after explicit GC.start - 514 MB
Using /home/charlie/.rvm/gems/ruby-1.9.3-p392
All memory used before 1st run - 7 MB
Memory consumed by 1st run of eat_up_memory - 439 MB
All memory used before 2nd run - 446 MB
Memory consumed by 2nd run of eat_up_memory - 4 MB
All memory used before 3rd run - 450 MB
Memory consumed by 3rd run of eat_up_memory - 4 MB
Memory used before explicit GC.start - 455 MB
Memory used after explicit GC.start - 455 MB
Using /home/charlie/.rvm/gems/ruby-2.0.0-p0
All memory used before 1st run - 7 MB
Memory consumed by 1st run of eat_up_memory - 439 MB
All memory used before 2nd run - 447 MB
Memory consumed by 2nd run of eat_up_memory - 58 MB
All memory used before 3rd run - 506 MB
Memory consumed by 3rd run of eat_up_memory - 18 MB
Memory used before explicit GC.start - 524 MB
Memory used after explicit GC.start - 524 MB
Using /home/charlie/.rvm/gems/ruby-1.9.3-p392
All memory used before 1st run - 7 MB
Memory consumed by 1st run of eat_up_memory - 439 MB
All memory used before 2nd run - 446 MB
Memory consumed by 2nd run of eat_up_memory - 4 MB
All memory used before 3rd run - 450 MB
Memory consumed by 3rd run of eat_up_memory - 14 MB
Memory used before explicit GC.start - 464 MB
Memory used after explicit GC.start - 464 MB
Using /home/charlie/.rvm/gems/ruby-2.0.0-p0
All memory used before 1st run - 7 MB
Memory consumed by 1st run of eat_up_memory - 439 MB
All memory used before 2nd run - 447 MB
Memory consumed by 2nd run of eat_up_memory - 58 MB
All memory used before 3rd run - 505 MB
Memory consumed by 3rd run of eat_up_memory - 19 MB
Memory used before explicit GC.start - 524 MB
Memory used after explicit GC.start - 524 MB
Using /home/charlie/.rvm/gems/ruby-1.9.3-p392
All memory used before 1st run - 7 MB
Memory consumed by 1st run of eat_up_memory - 439 MB
All memory used before 2nd run - 446 MB
Memory consumed by 2nd run of eat_up_memory - 4 MB
All memory used before 3rd run - 451 MB
Memory consumed by 3rd run of eat_up_memory - 15 MB
Memory used before explicit GC.start - 466 MB
Memory used after explicit GC.start - 466 MB
Using /home/charlie/.rvm/gems/ruby-2.0.0-p0
All memory used before 1st run - 7 MB
Memory consumed by 1st run of eat_up_memory - 439 MB
All memory used before 2nd run - 447 MB
Memory consumed by 2nd run of eat_up_memory - 55 MB
All memory used before 3rd run - 502 MB
Memory consumed by 3rd run of eat_up_memory - 22 MB
Memory used before explicit GC.start - 525 MB
Memory used after explicit GC.start - 525 MB
require 'securerandom'
def memory_usage
`ps -o rss= -p #{Process.pid}`.to_i
end
def profile_memory(comment)
before = memory_usage
block_given? ? yield : before = 0
puts "#{comment.ljust(50)} - #{(memory_usage - before) / 1024} MB"
end
def eat_up_memory
strings = []
1_000_000.times do
strings << SecureRandom.base64(255)
end
nil
end
['1st','2nd','3rd'].each do |count|
profile_memory "All memory used before #{count} run"
profile_memory("Memory consumed by #{count} run of eat_up_memory") do
eat_up_memory
end
end
profile_memory "Memory used before explicit GC.start"
GC.start
profile_memory "Memory used after explicit GC.start"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment