Thomas W.
Thomas W.

Reputation: 138

What is meant by generations of gc in python?

I'm new to python and still learning. Current I'm playing a bit with the gc module (garbage collector). One point is not clear to me: What is meant by generation? I know there are 3 of them, but what is the concept behind?

Upvotes: 3

Views: 767

Answers (1)

n00dl3
n00dl3

Reputation: 21574

Hoping it helps...

(Badly) translated from french wikipedia:

Every data don't have the same lifespan. Some of them have to be cleaned short time after they are created, other presist while your program is executing (global vars, etc). A solution would be to ask the program to "tag" these datas according to their probable lifespan. But it would be a very heavy solution, it's not unusual that data are created by libraries' functions, and we would need to provide their lifespan as a parameter.

The generation system is highly less invasive. The garbage collector create a hierarchy of several generations, from the youngest to the oldest. The newly created datas are generally placed in the youngest generation. Garbage are collected quite often in this young generation. Datas that havent been collected and destroyed in this generation are then moved to to an older one , etc. the idea is that datas with the shorter lifespan do not reach the the older generation (they can if they are allocated when they have just been allocated when they are found by the GC but it is rare).

Usually, there is 2 or 3 crescent generations, and a different algorithm is used for every generation, generally there is a non-incremential algorithm for the youngest generation because of its light weight, the garbage collection time is short. Older generations are collected with incremential algorithm.

Tweaking a generational garbage collector can be difficult. As the size of the youngest generation heavily impact the computation. The best choice depends on the application, CPU type and memory architecture

Upvotes: 1

Related Questions