Skip to content

Instantly share code, notes, and snippets.

@mikeckennedy
Last active March 7, 2022 04:10
Show Gist options
  • Save mikeckennedy/8934252da4774dec86790060bb9c80be to your computer and use it in GitHub Desktop.
Save mikeckennedy/8934252da4774dec86790060bb9c80be to your computer and use it in GitHub Desktop.
How much memory do Python threads use? Apparently not too much.
# 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()
@mikeckennedy
Copy link
Author

Output on macOS Monterey

10 threads, consuming 0.2 MB of memory total.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment