Ruby Memory leak (MRI)
<p>I must be missing something but every single application I write in Ruby seems like leaking some memory. I use Ruby MRI 2.3 but I see the same behaviour with other versions.</p>

<p>Whenever I write a test application that does something inside a loop it is slowly leaking memory.</p>

<pre><code>while true
   #do something
   sleep 0.1
end
</code></pre>

<p>For instance, I can write to array and then clean it in a loop, or just send http post request.</p>

<p>Here is just one example, but I have many examples like this:</p>

<pre><code>require 'net/http'
require 'json'
require 'openssl'

class Tester

    def send_http some_json
        begin
            @uri = URI('SERVER_URL')
            @http = Net::HTTP.new(@uri.host, @uri.port)
            @http.use_ssl = true
            @http.keep_alive_timeout = 10
            @http.verify_mode = OpenSSL::SSL::VERIFY_NONE
            @http.read_timeout = 30
            @req = Net::HTTP::Post.new(@uri.path, 'Content-Type' =&gt; 'application/json')
            @req.body = some_json.to_json
            res = @http.request(@req)
        rescue Exception =&gt; e  
                puts e.message  
                puts e.backtrace.inspect  
        end
    end

    def run
        while true
            some_json = {"name": "My name"}
            send_http(some_json)
            sleep 0.1
        end
    end
end


Tester.new.run
</code></pre>

<p>The leak I see is very small, it can be 0.5 mb every hour.</p>

<p>I ran the code with MemoryProfiler and with GC::Profiler.enable and it shows that I have no leaks. So it must be 2 options:</p>

<ol>
<li><p>There is a memory leak in C code. This might be possible but I don't use any external gems so I find it hard to believe that Ruby is leaking.</p></li>
<li><p>There is no memory leak and this is some sort of Ruby memory management mechanism. The thing is that I can defiantly see the memory growing. Until when will it grow? How much do I need to wait to know if it is a leak or now?</p></li>
</ol>

<p>The same code runs perfectly fine with JRuby without any leaks.</p>

<p>I was amazed reading a post:</p>

<p><a href="https://stackoverflow.com/questions/20385767/finding-the-cause-of-a-memory-leak-in-ruby">stack overlflow</a>
from Joe Edgar:</p>

<blockquote>
  <p>Ruby’s history is mostly as a command line tool for text processing
  and therefore it values quick startup and a small memory footprint. It
  was not designed for long-running daemon/server processes</p>
</blockquote>

<p>If what is written there is true and Ruby doesn't release memory back to OS then... We will always have a leak, right?</p>

<p>For instance:</p>

<ol>
<li>Ruby asks for memory from OS.</li>
<li>OS provides the memory to Ruby.</li>
<li>Ruby frees the memory but GC still didn't run.</li>
<li>Ruby asks for more memory from OS.</li>
<li>OS provide more memory to Ruby.</li>
<li>Ruby runs GC but it is too late as Ruby already asked twice.</li>
<li>And so on and on.</li>
</ol>

<p>What am I missing here?</p>