Created
November 27, 2025 19:54
-
-
Save sumantro93/d62b8a576cdeae9a6dbeb80a185edc19 to your computer and use it in GitHub Desktop.
memleak.py
This file contains hidden or 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
| import torch | |
| import gc | |
| def get_memory_mb(): | |
| import resource | |
| return resource.getrusage(resource.RUSAGE_SELF).ru_maxrss / 1024 | |
| initial_mem = get_memory_mb() | |
| print(f"Initial memory: {initial_mem:.2f} MB") | |
| # Repeated operations cause memory leak | |
| for i in range(10000): | |
| t = torch.randn(2, 3, 4, 5, 6) # 5 dims | |
| t = t.view(1, 2, 3, 4, 5, 6) # 6 dims - triggers leak | |
| gc.collect() # This is OUTSIDE the loop | |
| final_mem = get_memory_mb() | |
| print(f"Final memory: {final_mem:.2f} MB") | |
| print(f"Memory leaked: {final_mem - initial_mem:.2f} MB") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment