Last active
March 7, 2022 04:10
-
-
Save mikeckennedy/8934252da4774dec86790060bb9c80be to your computer and use it in GitHub Desktop.
How much memory do Python threads use? Apparently not too much.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Requires psutil package | |
import os | |
import threading | |
import time | |
import psutil | |
should_run = True | |
def main(): | |
global should_run | |
report_process_mem() | |
time.sleep(0.001) | |
start = report_process_mem() | |
thread_count = 10 | |
threads = [] | |
for _ in range(0, thread_count): | |
t = threading.Thread(target=thread_runner, daemon=True) | |
t.start() | |
threads.append(t) | |
end = report_process_mem() | |
mb = (end - start) | |
print(f'Started {thread_count:,} threads, consuming {mb:.01f} MB of memory total.', flush=True) | |
input("Enter to exit:") | |
should_run = False | |
[t.join() for t in threads] | |
def thread_runner(): | |
print(f"Started thread {threading.current_thread().native_id}...", flush=True) | |
while should_run: | |
time.sleep(.1) | |
print(f"Exited thread {threading.current_thread().native_id}.", flush=True) | |
def report_process_mem() -> int: | |
process = psutil.Process(os.getpid()) | |
mb = process.memory_info().rss / 1024 / 1024 | |
print(f"Total memory used: {mb:,.2f} MB.") | |
return mb | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output on macOS Monterey