Created
January 3, 2015 01:29
-
-
Save sb8244/0a7e505dd1dafe432b31 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Minimal example that demonstrates a memory leak in Celluloid. | |
# Adapted from @debrasetton example: https://github.com/mperham/sidekiq/issues/2107 | |
# Adapted from @grosser example: https://github.com/mperham/sidekiq/blob/master/examples/leak.rb | |
# Run with LEAK=0 for no memory leak or LEAK=1 for memory leak, like so: | |
# LEAK=1 ruby ./leak.rb > /dev/null | |
require 'celluloid/autostart' | |
Celluloid.logger = nil | |
$counter = 0 | |
$jobs = 5_000 | |
class MyWorker | |
include Celluloid | |
def perform | |
report_memory | |
check_done | |
# Normally, we'd perform some work here: | |
# ... | |
# Now, assume that the something went wrong for whatever reason and | |
# we'd like to retry the job. | |
if ENV['LEAK'].to_i == 1 | |
# If the error is raised using this line, it will cause a memory leak. | |
raise Exception.new("Exception - Job failed for some reason") | |
else | |
# On the other hand, if this line is used, memory usage remains ok. | |
raise "RuntimeError - Job failed for some reason" | |
end | |
end | |
def report_memory | |
$counter += 1 | |
if $counter % 100 == 0 | |
GC.start | |
memory = `ps -o rss -p #{Process.pid}`.chomp.split("\n").last.to_i | |
$stderr.puts "#{$counter.to_s.ljust(5)} - Memory usage: #{memory}" | |
end | |
end | |
def check_done | |
if $counter > $jobs | |
exit | |
end | |
end | |
end | |
pool = MyWorker.pool | |
# Enqueue jobs | |
$jobs.times { pool.async.perform } | |
while pool.busy_size > 0 | |
sleep(1) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
LEAK=0 bundle exec ruby ./script.rb > /dev/null | |
100 - Memory usage: 25388 | |
200 - Memory usage: 25672 | |
302 - Memory usage: 25704 | |
402 - Memory usage: 25704 | |
502 - Memory usage: 25740 | |
602 - Memory usage: 25984 | |
702 - Memory usage: 26112 | |
801 - Memory usage: 26144 | |
901 - Memory usage: 26136 | |
1000 - Memory usage: 26320 | |
1101 - Memory usage: 26312 | |
1201 - Memory usage: 26312 | |
1301 - Memory usage: 26312 | |
1400 - Memory usage: 26312 | |
1502 - Memory usage: 26320 | |
1602 - Memory usage: 26320 | |
1702 - Memory usage: 26312 | |
1802 - Memory usage: 26312 | |
1902 - Memory usage: 26312 | |
2003 - Memory usage: 26356 | |
2102 - Memory usage: 26388 | |
2200 - Memory usage: 26396 | |
2302 - Memory usage: 26404 | |
2400 - Memory usage: 26412 | |
2500 - Memory usage: 26412 | |
2600 - Memory usage: 26412 | |
2701 - Memory usage: 26432 | |
2802 - Memory usage: 26432 | |
2902 - Memory usage: 26468 | |
3002 - Memory usage: 26460 | |
3101 - Memory usage: 26460 | |
3202 - Memory usage: 26512 | |
3300 - Memory usage: 26512 | |
3402 - Memory usage: 26512 | |
3501 - Memory usage: 26512 | |
3601 - Memory usage: 26520 | |
3702 - Memory usage: 26512 | |
3800 - Memory usage: 26520 | |
3903 - Memory usage: 26520 | |
4001 - Memory usage: 26640 | |
4102 - Memory usage: 26632 | |
4202 - Memory usage: 26640 | |
4303 - Memory usage: 26640 | |
4400 - Memory usage: 26640 | |
4503 - Memory usage: 26640 | |
4600 - Memory usage: 26640 | |
4701 - Memory usage: 26640 | |
4801 - Memory usage: 26632 | |
4904 - Memory usage: 26632 | |
5000 - Memory usage: 26632 | |
LEAK=1 bundle exec ruby ./script.rb > /dev/null | |
102 - Memory usage: 28120 | |
200 - Memory usage: 33864 | |
302 - Memory usage: 39700 | |
401 - Memory usage: 45352 | |
503 - Memory usage: 51440 | |
600 - Memory usage: 56876 | |
700 - Memory usage: 62920 | |
802 - Memory usage: 68732 | |
901 - Memory usage: 74736 | |
1000 - Memory usage: 80360 | |
1100 - Memory usage: 86220 | |
1201 - Memory usage: 92096 | |
1301 - Memory usage: 97892 | |
1402 - Memory usage: 103660 | |
1502 - Memory usage: 109512 | |
1600 - Memory usage: 114952 | |
1702 - Memory usage: 121052 | |
1801 - Memory usage: 127256 | |
1901 - Memory usage: 132868 | |
2000 - Memory usage: 138472 | |
2101 - Memory usage: 144476 | |
2202 - Memory usage: 150176 | |
2301 - Memory usage: 155796 | |
2400 - Memory usage: 161428 | |
2502 - Memory usage: 167448 | |
2600 - Memory usage: 172836 | |
2701 - Memory usage: 178804 | |
2803 - Memory usage: 184696 | |
2902 - Memory usage: 190484 | |
3002 - Memory usage: 196080 | |
3101 - Memory usage: 201980 | |
3202 - Memory usage: 207812 | |
3300 - Memory usage: 213228 | |
3401 - Memory usage: 219296 | |
3500 - Memory usage: 224936 | |
3600 - Memory usage: 230856 | |
3701 - Memory usage: 237540 | |
3802 - Memory usage: 243492 | |
3900 - Memory usage: 249012 | |
4002 - Memory usage: 255104 | |
4100 - Memory usage: 260792 | |
4201 - Memory usage: 266544 | |
4302 - Memory usage: 272428 | |
4401 - Memory usage: 277904 | |
4502 - Memory usage: 284144 | |
4602 - Memory usage: 289648 | |
4703 - Memory usage: 295508 | |
4801 - Memory usage: 301120 | |
4902 - Memory usage: 307012 | |
5000 - Memory usage: 312684 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment