Python's requests library makes sending HTTP requests simple and convenient. However, one common issue developers face is memory leaks when using requests. This happens when requests keeps references to response objects in memory unnecessarily, preventing garbage collection.
Let's walk through a simple example to understand what's happening under the hood.
import requests
response = requests.get('https://example.com')
Here, we make a GET request and assign the response to
Over time, making repeated requests like this causes the process memory usage to grow continually - a memory leak.
The Solution - Close Connections
The easiest fix is closing the connection after we are done with the response by calling
import requests
response = requests.get('https://example.com')
# Process response
response.close()
Alternatively, we can use a context manager to automatically close connections:
with requests.get('https://example.com') as response:
# Process response
This ensures connections get closed promptly even if exceptions occur.
Other Recommendations
Following best practices like these prevents accidental memory leaks and makes