#6 – Memory Management in Python Flashcards

(11 cards)

1
Q

How is memory managed in Python?

A

Memory is managed automatically using reference counting and garbage collection.

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

What is reference counting in Python?

A

Every object has a counter of how many references point to it. When the count reaches zero, the object is deleted.

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

What is garbage collection in Python?

A

Garbage collector removes objects with circular references that are no longer accessible.

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

How do you check the reference count of an object?

A

Use sys.getrefcount().

import sys x = [] print(sys.getrefcount(x))
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

How do you manually run the garbage collector?

A

Use the gc module.

import gc gc.collect()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a memory leak in Python?

A

When objects are unintentionally kept alive, causing memory usage to grow.

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

What causes circular references?

A

When two or more objects reference each other, preventing automatic deletion by reference counting.

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

What is del used for in memory management?

A

It deletes a reference to an object. If it’s the last one, the object is garbage collected.

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

Do you always need to use del?

A

No, but you might use it to free large objects early or in tight loops.

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

How can context managers help with memory?

A

They ensure resources like files and connections are properly closed.

with open('file.txt') as f:     data = f.read()
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How can you investigate memory usage in Python?

A

Use libraries like tracemalloc, psutil, or objgraph for memory tracking and debugging.

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