To import this data, use the following command:
$ mongoimport --db time --collection holidays --upsert < holidays.json
Then run the leaktest.rb
like this (adding a Gemfile with the mongo
gem etc):
$ bundle exec ./leaktest.rb
You can then analyze the generated heap dump and get an output similar to this (gem install heapy
if it isn't already installed), which indicates that many objects are incorrectly retained on the heap:
$ heapy read /tmp/dump.json
Analyzing Heap
==============
Generation: nil object count: 45862
Generation: 59 object count: 8743
Generation: 60 object count: 3640
Generation: 61 object count: 3323
Generation: 62 object count: 2947
Generation: 63 object count: 2493
Generation: 64 object count: 1750
Generation: 65 object count: 1521
Generation: 66 object count: 845
Generation: 67 object count: 2111
Generation: 68 object count: 1382
Generation: 69 object count: 1239
Generation: 70 object count: 1183
Generation: 71 object count: 1183
Generation: 72 object count: 507
Generation: 73 object count: 3632
Generation: 74 object count: 2959
Generation: 75 object count: 2757
Generation: 76 object count: 2651
Generation: 77 object count: 2497
Generation: 78 object count: 2343
Generation: 79 object count: 2248
Generation: 80 object count: 2038
Generation: 81 object count: 1014
Generation: 82 object count: 2704
Generation: 83 object count: 1759
Generation: 84 object count: 1663
Generation: 85 object count: 1648
Generation: 86 object count: 676
Generation: 87 object count: 5010
Generation: 88 object count: 4008
Generation: 89 object count: 3826
Generation: 90 object count: 3549
Generation: 91 object count: 3380
Generation: 92 object count: 3211
Generation: 93 object count: 3042
Generation: 94 object count: 2873
Generation: 95 object count: 2704
Generation: 96 object count: 2535
Generation: 97 object count: 2366
Generation: 98 object count: 2257
Generation: 99 object count: 2137
Generation: 100 object count: 1014
Generation: 101 object count: 6855
Generation: 102 object count: 5533
Generation: 103 object count: 5249
Generation: 104 object count: 5009
Generation: 105 object count: 4623
Generation: 106 object count: 4443
Generation: 107 object count: 4116
Generation: 108 object count: 3887
Generation: 109 object count: 3718
Generation: 110 object count: 3549
Generation: 111 object count: 3320
Generation: 112 object count: 3102
Generation: 113 object count: 2934
Generation: 114 object count: 1400
Generation: 115 object count: 9355
Generation: 116 object count: 507
Generation: 117 object count: 3719
Generation: 118 object count: 11072
Generation: 119 object count: 2585
Generation: 120 object count: 90
If the get_connection
method is modified like this:
def get_connection
@client ||= begin
mongo_uri = 'mongodb://localhost/time'
options = {}
Mongo::Client.new(mongo_uri, options)
end
end
...the memory leak is worked around, and the heapy
session will instead look like this. A much lower number of retained objects:
$ heapy read /tmp/dump.json
Analyzing Heap
==============
Generation: nil object count: 45865
Generation: 59 object count: 8743
Generation: 60 object count: 3640
Generation: 61 object count: 3323
Generation: 62 object count: 2946
Generation: 63 object count: 1722
Generation: 128 object count: 8
Generation: 129 object count: 89
Generation: 130 object count: 825
Generation: 131 object count: 35