0
0
NumPydata~10 mins

Garbage collection and array references in NumPy - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Garbage collection and array references
Create array A
Assign B = A (reference)
Modify B affects A
Delete A
B still holds reference
Delete B
No references left -> Garbage collected
This flow shows how arrays share references, how deleting one reference doesn't delete the data until all references are gone, triggering garbage collection.
Execution Sample
NumPy
import numpy as np
A = np.array([1, 2, 3])
B = A
B[0] = 10
del A
del B
This code creates a numpy array, assigns it to another variable, modifies it, then deletes references to show when garbage collection happens.
Execution Table
StepActionArray AArray BReference CountGarbage Collection
1Create A[1, 2, 3]None1No
2Assign B = A[1, 2, 3][1, 2, 3]2 (shared)No
3Modify B[0] = 10[10, 2, 3][10, 2, 3]2 (shared)No
4Delete ADeleted[10, 2, 3]1No
5Delete BDeletedDeleted0Yes
💡 All references deleted at step 5, array memory is garbage collected.
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4After Step 5
A[1, 2, 3][1, 2, 3][10, 2, 3]DeletedDeleted
BNone[1, 2, 3][10, 2, 3][10, 2, 3]Deleted
Reference Count12210
Key Moments - 3 Insights
Why does modifying B also change A's values?
Because B is just another name (reference) for the same array as A, so changes via B affect the same data (see execution_table step 3).
Why isn't the array deleted when we delete A?
Deleting A only removes one reference; B still points to the array, so it stays in memory (see execution_table step 4).
When does garbage collection actually happen?
Garbage collection happens only when all references are deleted, here after deleting B at step 5 (see exit_note).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 3, what is the value of array A?
A[1, 2, 3]
BDeleted
C[10, 2, 3]
DNone
💡 Hint
Check the 'Array A' column at step 3 in execution_table.
At which step does the reference count drop to zero?
AStep 4
BStep 5
CStep 3
DStep 2
💡 Hint
Look at the 'Reference Count' column in variable_tracker after each step.
If we did not delete B at step 5, what would happen to the array?
AIt would remain in memory
BIt would be garbage collected anyway
CIt would cause an error
DIt would be copied automatically
💡 Hint
See explanation in key_moments about when garbage collection happens.
Concept Snapshot
Garbage collection in numpy depends on references.
Assigning one array to another shares the same data.
Modifying one reference changes the original array.
Deleting a reference removes one pointer.
Data is freed only when all references are deleted.
Use del to remove references and trigger garbage collection.
Full Transcript
This lesson shows how numpy arrays are managed in memory using references. When you assign one array to another variable, both variables point to the same data. Changing the array through one variable changes it for the other too. Deleting one variable only removes one reference; the data stays as long as another reference exists. Garbage collection happens only when all references are deleted, freeing the memory. This is important to understand to avoid unexpected changes and memory leaks.