macsash03
macsash03

Reputation: 19

Garbage Collector deletes an int but saves a list

I am curious why, if I make a list and an int and try to find it in gc.get_objects(), I found only the list. Code:

import gc

a = 1
b = [1, 2, 3]
for obj in gc.get_objects():
    if a is obj:
        print('Found: a')
    if b is obj:
        print('Found: b')
print(a)

Outputs only:

Found: b

1

There's no a in gc.get_objects() but of course I can still access it.

Upvotes: 0

Views: 53

Answers (1)

llllllllll
llllllllll

Reputation: 16434

You can read the python documentation here:

As a general rule, instances of atomic types aren’t tracked and instances of non-atomic types (containers, user-defined objects…) are.

To be noticed that this is not absolutely the case. This citation is also added to avoid misleading:

However, some type-specific optimizations can be present in order to suppress the garbage collector footprint of simple instances

Upvotes: 2

Related Questions