Python Forum
quick question about deleting an object from memory
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
quick question about deleting an object from memory
#1
Hi. So when you create an instance(I may be spelling this wrong) of a class you can add it to a list. You can also remove it from the list if you need to ,but my question is. When you do 
list.remove(object)
does that also delete the instance from the memory or is the instance of the class still callable? If so How can you delete the instance from the memory if you no longer need it? For example: you create a game, enter level 1, the level gets created and enemies spawn in, you finish the level so now you no longer need any of the objects that were created for level 1.
Reply
#2
Once there are no references to an object, the garbage collector will eventually purge those objects from memory.

In practice, that means using objects and functions, so there's an extremely small number of things that actually reference any memory.  To use your example, it'd be something like: 
if level_complete:
    sprites_in_level = []
    # any sprites that were loaded in that level are now not referenced, and will be garbage collected

    # load the sprites needed for the new level
    current_level += 1
    for sprite in LevelSprites(current_level):
        sprites_in_level.append(sprite.load())
Reply
#3
I see. so if I do this:
objects = []
new = (blockClass,'b1')
objects.append(new)
objects.remove(new)
#then assign new to a new instance of a class
new = (blockClass,'b2')
The first object will now no longer have any reference to it so it gets garbage collected, correct?
Reply
#4
Yes, the tuple that was originally assigned to "new" will be eventually garbage collected once you assign a new value to "new". "blockClass" will never be garbage collected, because there's now a new reference to it replacing the old reference.

...I say eventually, because the garbage collector rarely does things as soon as it possibly can. It waits a bit, and then removes things in waves.
Reply
#5
Yes when a object no longer referenced it will be garbage collected.
Remember that several object can point to same memory location.
One object can live on and one can be garbage collected,
even if the point to same memory location.
>>> objects = []
>>> new = ('b1')
>>> objects.append('hello')
>>> objects.append(new)
>>> # So now both point to same memory location
>>> id(new)
49629472
>>> id(objects[1])
49629472

# now "new" get a new memory location,and object in list is still pointing to same place
>>> new = ('999')
>>> id(new)
62396944
>>> id(objects[1])
49629472
The said end.
>>> objects[1] = 'world'
>>> objects
['hello', 'world']
>>> id(objects[1])
53206912
RIP 49629472 Sad
Reply
#6
Thank you. Sorry for the late reply I forgot that I needed to reply.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Quick question/help regarding my variable Extra 5 1,510 May-06-2022, 12:01 AM
Last Post: Extra
  Quick Question about Dictionaries Extra 6 1,798 Apr-29-2022, 08:34 PM
Last Post: Extra
  Memory Location, Object Attributes and how to use them Meld51 1 1,646 Aug-22-2021, 01:22 PM
Last Post: Yoriz
  finding and deleting json object GrahamL 1 4,839 Dec-10-2020, 04:11 PM
Last Post: bowlofred
  quick question/excel+python zarize 1 2,251 Dec-06-2019, 05:38 PM
Last Post: Larz60+
  opencv memory question djf123 1 4,612 May-26-2019, 11:03 AM
Last Post: heiner55
  A quick question teczone 4 3,053 Sep-06-2018, 03:44 PM
Last Post: snippsat
  Completely new to coding - quick question Oster22 1 2,693 Jun-19-2018, 08:42 PM
Last Post: Larz60+
  Quick help! Ellisrb 2 2,741 May-02-2018, 11:21 AM
Last Post: ThiefOfTime
  groups attribute of a groupby object question smw10c 2 4,300 Apr-27-2017, 03:18 PM
Last Post: smw10c

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020