open-telemetry / open-telemetry/opentelemetry-python
Virtual memory usage is high when imported with threads
@kennykguo is already working on this.
Since Jan 5, 2024.
- Dominant language
- Python
- Stars
- 2.6k
- Forks
- 1k
- Avg merge
- 4d 15h
- Merged PRs (30d)
- 19
Description
While importing from opentelemetry import metrics, consumes some memory ~136 KB (virt) and 0 Bytes (rss)
But with threads, the virtual memory usage is abnormally high - 4696MB virt mem.
My question is, why does the virtual memory shootup in this case and how can I manage it ?
In [1]: def imp():
...: from opentelemetry import metrics
...: mem = psutil.Process().memory_info()
...: imp()
...: print(ByteCount(psutil.Process().memory_info().vms - mem.vms), ByteCount(psutil.Process().memory_info().rss - mem.rss))
136 KiB 0 B
If I try to do the same with threads, the virtual memory usage shoots up abnormally
In [1]: def imp():
...: from opentelemetry import metrics
...:
...: mem = psutil.Process().memory_info()
...: threads = [threading.Thread(target=imp) for _ in range(200)]
...: for thread in threads: thread.start()
...: for thread in threads: thread.join()
...: print(ByteCount(psutil.Process().memory_info().vms - mem.vms), ByteCount(psutil.Process().memory_info().rss - mem.rss))
4696 MiB 652 KiB
I added threading.lock to prevent any race condition and make it thread sage. But the issue was still there
In [1]: lock = threading.Lock()
...: def imp():
...: with lock:
...: from opentelemetry import metrics
...:
...: mem = psutil.Process().memory_info()
...: threads = [threading.Thread(target=imp) for _ in range(200)]
...: for thread in threads: thread.start()
...: for thread in threads: thread.join()
...: print(ByteCount(psutil.Process().memory_info().vms - mem.vms), ByteCount(psutil.Process().memory_info().rss - mem.rss))
2456 MiB 992 KiB
I then added a condition to not import if it is already imported but still faced the same proble
In [1]: lock = threading.Lock()
...: def imp():
...: with lock:
...: if "opentelemetry" not in sys.modules:
...: from opentelemetry import metrics
...: print("Imported")
...:
...: mem = psutil.Process().memory_info()
...: threads = [threading.Thread(target=imp) for _ in range(200)]
...: for thread in threads: thread.start()
...: for thread in threads: thread.join()
...: print(ByteCount(psutil.Process().memory_info().vms - mem.vms), ByteCount(psutil.Process().memory_info().rss - mem.rss))
Imported
216 MiB 816 KiB
What is the expected behavior?
The expected virtual memory and rss increase shouldn't be as high when executed with threads.
What is the actual behavior?
The virtual memory usage is very high
Same is the case with from opentelemetry.sdk.metrics import MeterProvider too
Contributor guide
First steps
- Read the whole issue, then the project's contributing guide.
- Comment on the issue to say you are picking it up — it saves two people doing the same work.
- Fork the repository and make your change on a branch.
- Open a pull request that references the issue number.
Assessment
This issue has not been assessed yet.