<p dir="auto">I'm running Telegram bot and noticed the free memory degradation when running bot for a long time. Firstly, I suspect my code; then I suspect bot and finally I came to requests. :)<br>
I used <strong>len(gc.get_objects())</strong> to identify that problem exists. I located the communication routines, then cleared all bot code and comes to the example that raises the count of gc objects on every iteration.</p>
<h2 dir="auto">Expected Result</h2>
<p dir="auto"><strong>len(gc.get_objects())</strong> should give the <strong>same</strong> result on every loop iteration</p>
<h2 dir="auto">Actual Result</h2>
<p dir="auto">The value of <strong>len(gc.get_objects())</strong> increases on every loop iteration.</p>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="Test N2
GetObjects len: 27959
Test N3
GetObjects len: 27960
Test N4
GetObjects len: 27961
Test N5
GetObjects len: 27962
Test N6
GetObjects len: 27963
Test N7
GetObjects len: 27964"><pre class="notranslate"><code class="notranslate">Test N2
GetObjects len: 27959
Test N3
GetObjects len: 27960
Test N4
GetObjects len: 27961
Test N5
GetObjects len: 27962
Test N6
GetObjects len: 27963
Test N7
GetObjects len: 27964
</code></pre></div>
<h2 dir="auto">Reproduction Steps</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="token = &quot;XXX:XXX&quot;
chat_id = '111'
proxy = {'https':'socks5h://ZZZ'} #You may need proxy to run this in Russia

from time import sleep
import gc, requests

def garbage_info():
    res = &quot;&quot;
    res += &quot;\nGetObjects len: &quot; + str(len(gc.get_objects()))
    return res

def tester():
    count = 0
    while(True):
        sleep(1)
        count += 1
        msg = &quot;\nTest N{0}&quot;.format(count) + garbage_info()
        print(msg)

        method_url = r'sendMessage'
        payload = {'chat_id': str(chat_id), 'text': msg}

        request_url = &quot;https://api.telegram.org/bot{0}/{1}&quot;.format(token, method_url)
        method_name = 'get'

        session = requests.session()
        req = requests.Request(
            method=method_name.upper(),
            url=request_url,
            params=payload
        )
        prep = session.prepare_request(req)

        settings = session.merge_environment_settings(
            prep.url, None, None, None, None)
#            prep.url, proxy, None, None, None)  #Change the line to enable proxy
        send_kwargs = {
            'timeout': None,
            'allow_redirects': None,
        }
        send_kwargs.update(settings)
        resp = session.send(prep, **send_kwargs)

        # For more clean output
        gc.collect()

tester()"><pre class="notranslate"><code class="notranslate">token = "XXX:XXX"
chat_id = '111'
proxy = {'https':'socks5h://ZZZ'} #You may need proxy to run this in Russia

from time import sleep
import gc, requests

def garbage_info():
    res = ""
    res += "\nGetObjects len: " + str(len(gc.get_objects()))
    return res

def tester():
    count = 0
    while(True):
        sleep(1)
        count += 1
        msg = "\nTest N{0}".format(count) + garbage_info()
        print(msg)

        method_url = r'sendMessage'
        payload = {'chat_id': str(chat_id), 'text': msg}

        request_url = "https://api.telegram.org/bot{0}/{1}".format(token, method_url)
        method_name = 'get'

        session = requests.session()
        req = requests.Request(
            method=method_name.upper(),
            url=request_url,
            params=payload
        )
        prep = session.prepare_request(req)

        settings = session.merge_environment_settings(
            prep.url, None, None, None, None)
#            prep.url, proxy, None, None, None)  #Change the line to enable proxy
        send_kwargs = {
            'timeout': None,
            'allow_redirects': None,
        }
        send_kwargs.update(settings)
        resp = session.send(prep, **send_kwargs)

        # For more clean output
        gc.collect()

tester()
</code></pre></div>
<h2 dir="auto">System Information</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{
  &quot;chardet&quot;: {
    &quot;version&quot;: &quot;3.0.4&quot;
  },
  &quot;cryptography&quot;: {
    &quot;version&quot;: &quot;2.3.1&quot;
  },
  &quot;idna&quot;: {
    &quot;version&quot;: &quot;2.7&quot;
  },
  &quot;implementation&quot;: {
    &quot;name&quot;: &quot;CPython&quot;,
    &quot;version&quot;: &quot;3.6.6&quot;
  },
  &quot;platform&quot;: {
    &quot;release&quot;: &quot;4.15.0-36-generic&quot;,
    &quot;system&quot;: &quot;Linux&quot;
  },
  &quot;pyOpenSSL&quot;: {
    &quot;openssl_version&quot;: &quot;1010009f&quot;,
    &quot;version&quot;: &quot;17.5.0&quot;
  },
  &quot;requests&quot;: {
    &quot;version&quot;: &quot;2.19.1&quot;
  },
  &quot;system_ssl&quot;: {
    &quot;version&quot;: &quot;1010007f&quot;
  },
  &quot;urllib3&quot;: {
    &quot;version&quot;: &quot;1.23&quot;
  },
  &quot;using_pyopenssl&quot;: true
}"><pre class="notranslate"><code class="notranslate">{
  "chardet": {
    "version": "3.0.4"
  },
  "cryptography": {
    "version": "2.3.1"
  },
  "idna": {
    "version": "2.7"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.6.6"
  },
  "platform": {
    "release": "4.15.0-36-generic",
    "system": "Linux"
  },
  "pyOpenSSL": {
    "openssl_version": "1010009f",
    "version": "17.5.0"
  },
  "requests": {
    "version": "2.19.1"
  },
  "system_ssl": {
    "version": "1010007f"
  },
  "urllib3": {
    "version": "1.23"
  },
  "using_pyopenssl": true
}
</code></pre></div>
<p dir="auto">Summary.</p>
<h2 dir="auto">Expected Result</h2>
<p dir="auto">Program running normally</p>
<h2 dir="auto">Actual Result</h2>
<p dir="auto">Program consuming all ram till stops working</p>
<h2 dir="auto">Reproduction Steps</h2>
<p dir="auto">Pseudocode:</p>
<div class="highlight highlight-source-python notranslate position-relative overflow-auto" dir="auto" data-snippet-clipboard-copy-content="def function():
    proxies = {
        'https': proxy
    }
    session = requests.Session()
    session.headers.update({'User-Agent': 'user - agent'})
    try:                                           #
        login = session.get(url, proxies=proxies)  # HERE IS WHERE MEMORY LEAKS
    except:                                        #
        return -1                                  #
    return 0
"><pre class="notranslate"><span class="pl-k">def</span> <span class="pl-en">function</span>():
    <span class="pl-s1">proxies</span> <span class="pl-c1">=</span> {
        <span class="pl-s">'https'</span>: <span class="pl-s1">proxy</span>
    }
    <span class="pl-s1">session</span> <span class="pl-c1">=</span> <span class="pl-s1">requests</span>.<span class="pl-v">Session</span>()
    <span class="pl-s1">session</span>.<span class="pl-s1">headers</span>.<span class="pl-en">update</span>({<span class="pl-s">'User-Agent'</span>: <span class="pl-s">'user - agent'</span>})
    <span class="pl-k">try</span>:                                           <span class="pl-c">#</span>
        <span class="pl-s1">login</span> <span class="pl-c1">=</span> <span class="pl-s1">session</span>.<span class="pl-en">get</span>(<span class="pl-s1">url</span>, <span class="pl-s1">proxies</span><span class="pl-c1">=</span><span class="pl-s1">proxies</span>)  <span class="pl-c"># HERE IS WHERE MEMORY LEAKS</span>
    <span class="pl-k">except</span>:                                        <span class="pl-c">#</span>
        <span class="pl-k">return</span> <span class="pl-c1">-</span><span class="pl-c1">1</span>                                  <span class="pl-c">#</span>
    <span class="pl-k">return</span> <span class="pl-c1">0</span></pre></div>
<h2 dir="auto">System Information</h2>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="$ python -m requests.help"><pre class="notranslate"><code class="notranslate">$ python -m requests.help
</code></pre></div>
<div class="snippet-clipboard-content notranslate position-relative overflow-auto" data-snippet-clipboard-copy-content="{
  &quot;chardet&quot;: {
    &quot;version&quot;: &quot;3.0.4&quot;
  },
  &quot;cryptography&quot;: {
    &quot;version&quot;: &quot;&quot;
  },
  &quot;idna&quot;: {
    &quot;version&quot;: &quot;2.6&quot;
  },
  &quot;implementation&quot;: {
    &quot;name&quot;: &quot;CPython&quot;,
    &quot;version&quot;: &quot;3.6.3&quot;
  },
  &quot;platform&quot;: {
    &quot;release&quot;: &quot;10&quot;,
    &quot;system&quot;: &quot;Windows&quot;
  },
  &quot;pyOpenSSL&quot;: {
    &quot;openssl_version&quot;: &quot;&quot;,
    &quot;version&quot;: null
  },
  &quot;requests&quot;: {
    &quot;version&quot;: &quot;2.18.4&quot;
  },
  &quot;system_ssl&quot;: {
    &quot;version&quot;: &quot;100020bf&quot;
  },
  &quot;urllib3&quot;: {
    &quot;version&quot;: &quot;1.22&quot;
  },
  &quot;using_pyopenssl&quot;: false
}"><pre class="notranslate"><code class="notranslate">{
  "chardet": {
    "version": "3.0.4"
  },
  "cryptography": {
    "version": ""
  },
  "idna": {
    "version": "2.6"
  },
  "implementation": {
    "name": "CPython",
    "version": "3.6.3"
  },
  "platform": {
    "release": "10",
    "system": "Windows"
  },
  "pyOpenSSL": {
    "openssl_version": "",
    "version": null
  },
  "requests": {
    "version": "2.18.4"
  },
  "system_ssl": {
    "version": "100020bf"
  },
  "urllib3": {
    "version": "1.22"
  },
  "using_pyopenssl": false
}
</code></pre></div>