Consider some Python 3 code where we have a very large object, and I need to store a reference to this object within a class due to third-party libraries.
In this case, the object df
is a very large numpy array (>20 GB) and my system's memory is already almost maxed out. Does assigning df
as an instance attribute duplicate the memory required, or simply act as a reference to the existing object?
In code:
import numpy as np
df = np.random.rand(1000, 5) # Some large numpy array
class MyClass:
def __init__(self, df):
self.df = df
def get_first(self):
return self.df[0]
instance = MyClass( df ) # Does this copy the object `df`?
I'm hitting some memory issues later on and trying to debug where it might be coming from. My intuition tells me that Python knows to not copy the object, however if we do something like del df
then instance.df
is still defined.