Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6011771
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T02:18:27+00:00 2026-05-23T02:18:27+00:00

There are several threads on Python garbage collection in SO, and after reading about

  • 0

There are several threads on Python garbage collection in SO, and after reading about five, plus some doc on line, i am still not sure as to how garbage collection works and how i should manage objects which i am not using. In fact somewhere i read one should not do anything about collecting garbage, others tell one should del objects, while others again explain de-referencing an object is enough for Python to collect it as garbage.

So, at the risk of creating a duplicate, i will ask the question again, but differently, hoping to get more comprehensive and clearer information.

In my case i want to make a small simulation with objects representing people. Several instances of the Person() class will be created. It should exist for some time until it virtually “dies” while other instances will be created.

Now how do i make this Person() instance “die” (assuming many many of these instances will be created and i don’t want these instances to hang out like ghosts)?

There are several ways i can reference an object:

john = Person('john')

or

people = []
people.append(Person('john'))

or

people = {}
people['john'] = Person('john')

What is the best way to keep my program clean, freeing resources optimally? And what is the best way then to reference my object so i can control the deletion of the object?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T02:18:27+00:00Added an answer on May 23, 2026 at 2:18 am

    I find that most programs create and dispose of objects quite naturally, so I never normally worry about it.

    Some examples:

    person = Person('john')
    person = Person('james')
    # Whoops! 'john' has died!
    
    people = []
    people.append(Person('john'))
    # ...
    # All 'Persons' live in people
    people = []
    # Now all 'Persons' are dead (including the list that referenced them)
    
    class House():
        def setOwner(self, person):
            self.owner = person
    
    house.setOwner(people[0])
    # Now a House refers to a Person
    people = []
    # Now all 'Persons' are dead, except the one that house.owner refers to.
    

    What I assume you are after is this:

    people = {}
    people['john'] = Person('john')
    
    def removePerson(personName):
        del people[personName]
    
    removePerson('john')
    

    In this case people is the master list and you can control when a Person gets added and removed from the list (its a dictionary).

    You may have to think through the concept of a person being created and then dying very thoroughly: Once created how does the person first interact with the simulation. Upon death, how should you untangle the references? (Its ok for a person to refer to other stuff, its things like House in my example that would keep a person alive. You could have other objects hold on to just the name of the person).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know that there are several threads posted about this issue, however I believe
I know there are several threads and posts regarding this issue in the internet
I have a Map which is to be modified by several threads concurrently. There
There are several people about (myself being one of them) who have been developing
There are several topics about what questions will be raised in an interview for
After seeing several threads rubbishing the regexp method of finding a term to match
In a multi-thread code, if there are several threads trying to send data to
There are several threads in my application that work in the background. They connect
I know there are several threads on this already, but I've hit a brick
I'm aware that there are several threads with the same problem, but i haven't

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.