I don't exactly understand how python use memory, and what Python gives with the tracemalloc builtins. Let's take this exemple:
import tracemalloc
import gc
L=[1,2,3]
tracemalloc.start()
gc.collect()
L += [4]
a = L[:]
b = [_ for _ in L]
del(L)
snapshot = tracemalloc.take_snapshot() # 1° way to get memory consumption
top_stats = snapshot.statistics('traceback')
for stat in top_stats:
print(stat)
#print(tracemalloc.get_traced_memory()) # 2° way to get memory consumption
tracemalloc.stop()
With the 1° method I get:
D:\file.py:13: size=504 B, count=3, average=168 B -> for 'b'
D:\file.py:20: size=416 B, count=1, average=416 B -> for 'snapshot' (useless)
D:\file.py:12: size=88 B, count=2, average=44 B -> for 'a'
and with the 2° one: (592, 784) # (Courant,Peak)
So we have Courant which is the sum of memory for 'a' and 'b'. But Peak > Courant so why don't we have the additional memory consumptions given with take_snapshot() builtin ? When one uses some functions, take_snapshot() tells how much memory had been use inside. Why not here?
Secondly, about the values in themselves. Why take_snapshot() gives 2 different sizes for 'a' and for 'b' (88 and 504) ? And we get sys.getsizeof(a)=88 and sys.getsizeof(b)=88. So what does size=504 for 'b' mean?
Finally, why L += [4] doesn't take any memory space? More simply, if one does c=12 or c=12.0 or c='azerty' or c=(1,2) ... (every instanciation of not mutable objects, I would say), it's invisible for both functions of 'tracemalloc'. However it isn't for sys.getsizeof(.)!
So I don't understand how I can found the memory consumption (Courant, Peak and line-by-line).
Thanks for your answers!