The documentation for python's gc package says this about gc.get_count():
gc.get_count()
Return the current collection counts as a tuple of (count0, count1, count2).
Here is a sample program:
import gc
if __name__=="__main__":
print("making some data")
for k in range(10):
root = [range(i,1000) for i in range(1,1000)]
print("len(gc.get_objects):",len(gc.get_objects()))
print("gc.get_stats:",gc.get_stats())
print("gc.get_count:",gc.get_count())
Here is the output:
making some data
len(gc.get_objects): 7130
gc.get_stats: [{'collections': 16, 'collected': 99, 'uncollectable': 0}, {'collections': 1, 'collected': 0, 'uncollectable': 0}, {'collections': 0, 'collected': 0, 'uncollectable': 0}]
gc.get_count: (75, 5, 1)
Clearly, count0 = 75, count1=5, and count2=1.
What are count0, count1 and count2?
Unfortunately both the answer given by @user10637953, and the referenced article are incorrect.
count0
are the (tracked object allocations - deallocations) that happened since last garbage collection.
Sometime after it reaches the gen0 threshold (defaults to 700), a gen0 garbage collection will occur, and count0
will reset.
count1
is the number of gen0 collections since the last gen1 collection. Upon reaching the threshold (defaults to 10), gen1 collection will occur, and count1
will reset.
count2
is the number of gen1 collections since the last gen2 collection. Upon reaching the threshold (defaults to 10 as well), gen2 collection will occur, and count2
will reset.
You can easily prove this yourself by running gc.collect(gen)
, and see the thresholds using gc.get_threshold()
.
For more info, see the official dev guide.
'gc' in the gc package refers to 'garbage collection'. It is a set of hands-on methods by which you can take over garbage collection duties from the system. Doing so can free up resources and streamline the efficiency of your program execution. But it should be noted that the system garbage collection management is already pretty dang good. Yes, the system creates a bunch of objects and other overhead for every variable you create and reference, and yes that overhead can linger for a long time (even if you call a del command), but on the whole it does no harm.
gc.get_count() returns a tuple indicating the internal housekeeping counts of object-references that the system is keeping. These are the counts by which the system will know when it is OK to (automatically) collect the garbage.
Each of the tuple's three values represent objects (references) in three generations. Python keeps a list of three generations in all. Each time an object survives a garbage collection event, it is move up to the next generation. (75, 5, 1) mean there are 75 objects in the newest generation, 5 objects in the middle generation and 1 object in the oldest generation.
The counts are internal system values and not values you assign or have (much) direct control over. You can reset some values using gc.collect(), freeing up large blocks of resources; and you can set the trigger values the system uses to trip garbage collection events, using gc.get_threshold() and gc.set_threshold(). But for all but the hardest-core coders and developers (of which I certainly am not one) garbage collection is best left on the hands of the python system.
Does this answer the question?
Credit: The information on the gc.get_count() method was gleaned from stactify.com/python-garbage-collection/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With