Python-Tls-Client
Python-Tls-Client copied to clipboard
Memory Leak
When making requests that return large responses, memory usage goes through the roof. It doesn’t seem to scale properly with the response size.
Script to reproduce
import psutil
import tls_client
def memory_usage():
return psutil.Process().memory_info().rss // (1024 ** 2)
print(f"Before request: {memory_usage()}MB")
response = tls_client.Session().get(
url="https://platform.tonybet.com/api/event/list",
params={
'limit': 200,
'main': 0,
'page': 1,
'oddsExists_eq': 1,
'period': 0,
'lang': 'en',
'relations[]': [
'odds',
'competitors',
'players',
'variants',
'league',
'sport'
],
},
)
print(f"After request: {memory_usage()}MB")
print(f"Response size: {len(response.text.encode()) // (1024 ** 2)}MB")
Output
Before request: 27MB
After request: 1470MB
Response size: 73MB
Expected Behavior A 73MB response shouldn’t cause memory usage to jump by over 1GB. Some increase is expected, but this seems excessive.
This issue appears to be caused by the underlying dependency bogdanfinn/tls-client, rather than this project itself.
It has been discussed in the upstream repository here:
bogdanfinn/tls-client#165
The maintainer responded with the assumption that the increased memory usage may not be a true memory leak, but rather due to how Python's garbage collector works in combination with how the library is used (especially in the main process or without proper scoping).
They suggest wrapping memory-heavy calls in separate functions to allow Python to release memory more effectively, and confirmed that freeMemory() should be called to help clean up.
For reference, here is their comment and workaround:
https://github.com/bogdanfinn/tls-client/issues/165#issuecomment-2723891042