Requests memory leak
See original GitHub issueSummary.
Expected Result
Program running normally
Actual Result
Program consuming all ram till stops working
Reproduction Steps
Pseudocode:
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
System Information
$ python -m requests.help
{
"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
}
Issue Analytics
- State:
- Created 5 years ago
- Reactions:7
- Comments:22 (2 by maintainers)
Top Results From Across the Web
Memory leaks in requests library - python - Stack Overflow
To fit all this in memory, Python requests more memory to be allocated from your OS. Once the detection process is complete, that...
Read more >How To Detect and Prevent Memory Leaks | Scout APM Blog
Thus, we can say that memory leak occurs in python when the unused data is heaped up and the programmer forgets to delete...
Read more >Cause of Memory Leak in Python And How to Fix Them
Different types of references are used in code referencing, with the additional capabilities to collect garbage. The referencing style would evaluate the memory...
Read more >Debugging and preventing memory errors in Python - Medium
This case is essentially the same case outlined in the intro: a server runs out of memory while processing a request. The dangerous...
Read more >Diagnosing and Fixing Memory Leaks in Python - Fugue
tracemalloc , a powerful memory tracking tool in the Python standard library, made it possible to quickly diagnose and fix the leak. We ......
Read more >Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start FreeTop Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Top GitHub Comments
Call
Session.close()
andResponse.close()
can avoid the memory leak. And ssl will consume more memory so the memory leak will more remarkable when request https urls.First I make 4 test cases:
Pseudo code:
Memory usage graph(y-axis: MB, x-axis: time), requests use lots of memory and memory increase very fast, while aiohttp memory usage is stable:
Then I add
Session.close()
and test again:Memory usage significant decreased, but memory usage still increase over time:
Finally I add
Response.close()
and test again:Memory usage decreased again, and not increase over time:
Compare aiohttp and requests shows memory leak is not caused by ssl, it’s caused by connection resources not closed.
Useful scripts:
System Information:
Similar issue. Requests eats memory when running in thread. Code to reproduce here:
In the code given above I pass a session object around, but if I replace it with just running
requests.get
nothing changes.Output is:
And Pipfile looks like this: