0

I have a list of large-sized objects. The code below is purely for explanation purposes. self.allocate_memory() can be implemented accordingly.

class BigObject(object):
   def __init__(self, id):
     self.id = id
     self.allocate_memory(100) # allocates 100 MB of memory to self


BigList = [BigObject(x) for x in range(10000)]
BigList[2] = BigObject(-99) # is this action deleting the memory allocated to BigObject(3)

When I assign a new object to the index in the list, is the memory of object being replaced deallocated? If not, is there a way to ensure this?

pg2455
  • 5,039
  • 14
  • 51
  • 78
  • Is this a serious question? _Of course_ python disposes of objects that are no longer needed. (Do keep in mind though that "no longer needed" isn't necessarily the same thing as "it's been removed from `BigList`". If any other references to `BigObject(3)` exist, it obviously won't be deallocated.) – Aran-Fey Mar 07 '18 at 11:16
  • 1
    Possible duplicate of [When are objects garbage collected in python?](https://stackoverflow.com/questions/9449489/when-are-objects-garbage-collected-in-python) – Aran-Fey Mar 07 '18 at 11:20

0 Answers0