0
0
NumPydata~5 mins

Garbage collection and array references in NumPy

Choose your learning style9 modes available
Introduction
Garbage collection helps clean up memory by removing data no longer needed. Array references let you share or copy data without extra memory use.
When you want to avoid using extra memory by sharing the same array in different parts of your program.
When you want to make a copy of an array to change it without affecting the original.
When you want to understand why changing one array also changes another.
When you want to make sure unused arrays are removed to free memory.
Syntax
NumPy
import numpy as np

# Create an array
original_array = np.array([1, 2, 3])

# Reference assignment (no new array created)
reference_array = original_array

# Copy array (new array created)
copied_array = original_array.copy()
Assigning one array to another variable creates a reference, not a new array.
Using .copy() creates a new array in memory, independent of the original.
Examples
Shows that even empty or single-element arrays can be referenced. Changing the reference changes the original array.
NumPy
import numpy as np

# Empty array reference
empty_array = np.array([])
reference_empty = empty_array

# One element array reference
one_element_array = np.array([10])
reference_one_element = one_element_array

# Changing reference affects original
reference_one_element[0] = 20
Changing the copied array does not affect the original array.
NumPy
import numpy as np

# Copying array
original_array = np.array([5, 6, 7])
copied_array = original_array.copy()
copied_array[0] = 100
Changing array_b changes array_a because they reference the same data.
NumPy
import numpy as np

# Reference assignment
array_a = np.array([1, 2, 3])
array_b = array_a
array_b[1] = 99
Sample Program
This program shows how changing a reference affects the original array, but changing a copy does not.
NumPy
import numpy as np

# Create original array
original_array = np.array([10, 20, 30])
print("Original array before changes:", original_array)

# Create reference to original array
reference_array = original_array

# Create copy of original array
copied_array = original_array.copy()

# Change reference array
reference_array[0] = 100
print("Original array after changing reference:", original_array)

# Change copied array
copied_array[1] = 200
print("Copied array after change:", copied_array)
print("Original array after changing copy:", original_array)
OutputSuccess
Important Notes
Time complexity for copying an array is O(n), where n is the number of elements.
Assigning a reference is O(1) because no data is copied.
A common mistake is thinking assignment creates a new array; it only creates a new reference.
Use copying when you want to change data without affecting the original array.
Summary
Assigning one array to another variable creates a reference to the same data.
Changing a reference changes the original array because they share data.
Use .copy() to create a new independent array to avoid unwanted changes.
Garbage collection frees memory when arrays are no longer referenced.