I have the following code:
import gc
import resource
import time
gc.set_debug(gc.DEBUG_SAVEALL)
while True:
class A():
pass
print(resource.getrusage(resource.RUSAGE_SELF).ru_maxrss)
print("Garbage: ", len(gc.garbage))
print("Collected: ", gc.collect())
time.sleep(1)
and when i run it for a few iterations, i get the following:
9916
Garbage: 0
Collected: 0
9916
Garbage: 0
Collected: 5
9916
Garbage: 5
Collected: 5
9916
Garbage: 10
Collected: 5
9916
Garbage: 15
Collected: 5
As we can see, the garbage in memory starts to grow and the garbage collector can't collect the allocated memory. As a result, we have just created a memory leak.
My question is why this memory leak happens?
Does it have to do with the way class definitions are stored in the heap?