Skip to content

Instantly share code, notes, and snippets.

@sumantro93
Created November 27, 2025 19:54
Show Gist options
  • Select an option

  • Save sumantro93/d62b8a576cdeae9a6dbeb80a185edc19 to your computer and use it in GitHub Desktop.

Select an option

Save sumantro93/d62b8a576cdeae9a6dbeb80a185edc19 to your computer and use it in GitHub Desktop.
memleak.py
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