Python Garbage Collection Flashcards

(45 cards)

1
Q

What is the primary purpose of a garbage collector in Python?

A

To automatically manage memory by reclaiming memory occupied by objects that are no longer in use.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

True or False: Python uses reference counting as its primary garbage collection mechanism.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Fill in the blank: The process of identifying and freeing memory occupied by objects that are no longer referenced is called _______.

A

garbage collection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is reference counting?

A

A technique where each object keeps track of the number of references pointing to it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What happens when an object’s reference count reaches zero?

A

The memory occupied by the object is reclaimed by the garbage collector.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Which module in Python provides an interface to the garbage collection facilities?

A

The ‘gc’ module.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

True or False: Cyclic references can be handled by Python’s garbage collector.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is a cyclic reference?

A

A situation where two or more objects reference each other, creating a cycle that prevents reference counting from reaching zero.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

How does Python’s garbage collector handle cyclic references?

A

It uses a technique called cycle detection to identify and collect objects involved in cycles.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What is the function used to manually trigger garbage collection in Python?

A

gc.collect()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

Fill in the blank: The default garbage collection strategy in Python is based on _______ and _______.

A

reference counting; cycle detection

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

What is the ‘gc.set_debug()’ function used for?

A

To enable debugging options for the garbage collector.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What does the ‘gc.get_objects()’ function return?

A

A list of all objects tracked by the garbage collector.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

True or False: Garbage collection can lead to performance issues if not managed properly.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

What is the purpose of the ‘gc.collect()’ method?

A

To force a garbage collection of unreachable objects.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What does the ‘gc.get_stats()’ function provide?

A

Statistics about the current state of the garbage collector.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
17
Q

What is ‘finalization’ in the context of garbage collection?

A

The process of calling an object’s finalizer method before the object is reclaimed.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
18
Q

True or False: Python’s garbage collector can be disabled.

A

True

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
19
Q

What is the command to disable Python’s garbage collector?

20
Q

What is the impact of disabling the garbage collector?

A

It can lead to increased memory usage and potential memory leaks.

21
Q

What is the purpose of weak references in Python?

A

To allow the garbage collector to reclaim an object without preventing its memory from being freed.

22
Q

What module provides support for weak references?

A

The ‘weakref’ module.

23
Q

Fill in the blank: Objects created with weak references can still be _______ by the garbage collector.

24
Q

What is a ‘weak reference’?

A

A reference to an object that does not increase its reference count.

25
What is the function of 'gc.get_referrers()'?
To return a list of objects that refer to a given object.
26
True or False: Python's garbage collector is non-deterministic.
True
27
What is the significance of the 'gc.garbage' list?
It contains a list of objects that the garbage collector found to be unreachable but cannot be freed.
28
What is the purpose of the '__del__' method?
To define a finalizer for an object that is called when the object is about to be destroyed.
29
True or False: The '__del__' method is guaranteed to be called when an object is garbage collected.
False
30
What can prevent the '__del__' method from being executed?
If there are circular references involving the object.
31
What is the 'gc.get_count()' function used for?
To return the current collection counts for the garbage collector.
32
Fill in the blank: The garbage collector is part of the _______ module in Python.
gc
33
What does 'gc.collect(generation)' do?
It collects garbage from the specified generation.
34
What are the three generations in Python's garbage collection?
Young, middle-aged, and old generations.
35
True or False: Older objects are collected more frequently than younger objects in Python's garbage collection.
False
36
What is the 'threshold' in Python's garbage collection?
The number of allocations and deallocations that trigger a collection cycle.
37
How can you change the garbage collection thresholds?
Using the 'gc.set_stats()' method.
38
What is the default garbage collection threshold for generation 0?
700
39
True or False: Python does not allow for custom garbage collection strategies.
False
40
What is the potential downside of relying solely on garbage collection?
It can lead to unpredictable performance and latency in memory management.
41
What is the role of the 'gc' module in Python?
To provide an interface for interacting with the garbage collection process.
42
Fill in the blank: The Python garbage collector uses _______ to detect cycles.
tracing
43
What happens during a garbage collection cycle?
The collector identifies unreachable objects and reclaims their memory.
44
What is the purpose of the 'gc.collect(generation)' function?
To explicitly trigger garbage collection for a specific generation.
45
True or False: Memory leaks are impossible in Python due to garbage collection.
False