Garbage collection and array references in NumPy - Time & Space Complexity
When working with arrays in numpy, understanding how references and garbage collection affect performance is important.
We want to see how the program's work changes as array sizes grow and references are created or removed.
Analyze the time complexity of the following code snippet.
import numpy as np
a = np.arange(1000000)
b = a
c = a.copy()
d = None
This code creates a large array, assigns references, makes a copy, and removes a reference.
- Primary operation: Copying the array with
a.copy()which duplicates all elements. - How many times: The copy operation touches each element once, so it runs once over all elements.
Copying the array means touching every element, so the work grows as the array gets bigger.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | 10 operations |
| 100 | 100 operations |
| 1000 | 1000 operations |
Pattern observation: The work grows directly with the number of elements; doubling elements doubles the work.
Time Complexity: O(n)
This means the time to copy the array grows linearly with the number of elements.
[X] Wrong: "Assigning b = a copies the array and takes time proportional to its size."
[OK] Correct: Assigning b = a just creates a new reference to the same array without copying data, so it is very fast and does not depend on array size.
Understanding how references and copying affect performance helps you write efficient code and explain memory use clearly in interviews.
"What if we used b = a.view() instead of a.copy()? How would the time complexity change?"
Practice
.copy()?Solution
Step 1: Understand assignment behavior in NumPy
Assigning one array to another variable without.copy()does not create a new array but a reference to the same data.Step 2: Effect of shared references
Both variables point to the same memory location, so changes via one variable affect the other.Final Answer:
Both variables refer to the same array data in memory. -> Option CQuick Check:
Assignment without copy = shared reference [OK]
- Thinking assignment creates a new array copy
- Assuming changes to one variable don't affect the other
- Confusing assignment with .copy() method
a?Solution
Step 1: Identify the method for copying arrays
The.copy()method creates a new independent array with its own data.Step 2: Check other options
Assignmentb = ashares data;copy(a)is invalid without import; slicinga[:]creates a view, not a full copy.Final Answer:
b = a.copy() -> Option AQuick Check:
Use .copy() for independent array [OK]
- Using simple assignment expecting a copy
- Using slicing which creates a view, not a copy
- Calling copy() without importing copy module
import numpy as np x = np.array([1, 2, 3]) y = x y[0] = 10 print(x)
What is the output?
Solution
Step 1: Understand reference assignment
Variableyis assigned tox, so both refer to the same array.Step 2: Effect of modifying y on x
Changingy[0]to 10 changes the shared array, sox[0]is also 10.Final Answer:
[10 2 3] -> Option AQuick Check:
Shared reference means change reflects in both [OK]
- Expecting original array to remain unchanged
- Confusing assignment with copy
- Thinking print will show error
import numpy as np x = np.array([1, 2, 3]) y = x[:] y[0] = 5 print(x)
Solution
Step 1: Understand slicing behavior in NumPy
Slicing an array creates a view, not a copy, soyshares data withx.Step 2: Effect of modifying y on x
Changingy[0]to 5 modifies the shared data, sox[0]also becomes 5.Final Answer:
Slicing creates a view, so modifying y changes x. -> Option DQuick Check:
Slicing = view, not copy [OK]
- Assuming slicing creates a copy
- Expecting original array to stay unchanged
- Thinking slicing causes syntax error
a used in multiple variables. You want to free memory used by a when no longer needed. Which statement is TRUE about garbage collection in this context?Solution
Step 1: Understand Python garbage collection
Python frees memory of objects when no references to them remain.Step 2: Effect of references on NumPy arrays
NumPy arrays are freed automatically when all variables referencing them are deleted or reassigned.Final Answer:
Deleting all references toaallows Python to free its memory automatically. -> Option BQuick Check:
Garbage collection frees unreferenced arrays [OK]
- Thinking manual memory free is needed
- Assuming assigning None frees memory if other refs exist
- Believing NumPy arrays are exempt from garbage collection
Ready to prove it?
Pass the test and this pattern moves unknown
