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

  • SEARCH
  • Home
  • 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 9181161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T18:13:17+00:00 2026-06-17T18:13:17+00:00

I create many object then I store in a list. But I want to

  • 0

I create many object then I store in a list. But I want to delete them after some time because I create news one and don’t want my memory goes high (in my case, it jumps to 20 gigs of ram if I don’t delete it).

Here is a little code to illustrate what I trying to do:

class test:
    def __init__(self):
        self.a = "Hello World"
    def kill(self):
        del self

a = test()
b = test()
c = [a,b]

print("1)Before:",a,b)

for i in c:
    del i

for i in c:
    i.kill()   

print("2)After:",a,b)

A and B are my objects. C is a list of these two objects. I’m trying to delete it definitely with a for-loop in C: one time with DEL and other time with a function. It’s not seem to work because the print continue to show the objects.

I need this because I create 100 000 objects many times. The first time I create 100k object, the second time another 100k but I don’t need to keep the previous 100k. If I don’t delete them, the memory usage goes really high, very quickly.

  • 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-06-17T18:13:18+00:00Added an answer on June 17, 2026 at 6:13 pm

    tl;dr;

    mylist.clear()  # Added in Python 3.3
    del mylist[:]
    

    are probably the best ways to do this. The rest of this answer tries to explain why some of your other efforts didn’t work.


    cpython at least works on reference counting to determine when objects will be deleted. Here you have multiple references to the same objects. a refers to the same object that c[0] references. When you loop over c (for i in c:), at some point i also refers to that same object. the del keyword removes a single reference, so:

    for i in c:
       del i
    

    creates a reference to an object in c and then deletes that reference — but the object still has other references (one stored in c for example) so it will persist.

    In the same way:

    def kill(self):
        del self
    

    only deletes a reference to the object in that method. One way to remove all the references from a list is to use slice assignment:

    mylist = list(range(10000))
    mylist[:] = []
    print(mylist)
    

    Apparently you can also delete the slice to remove objects in place:

    del mylist[:]  #This will implicitly call the `__delslice__` or `__delitem__` method.
    

    This will remove all the references from mylist and also remove the references from anything that refers to mylist. Compared that to simply deleting the list — e.g.

    mylist = list(range(10000))
    b = mylist
    del mylist
    #here we didn't get all the references to the objects we created ...
    print(b) #[0, 1, 2, 3, 4, ...]
    

    Finally, more recent python revisions have added a clear method which does the same thing that del mylist[:] does.

    mylist = [1, 2, 3]
    mylist.clear()
    print(mylist)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically I want to create one large object of many object in JavaScript. Something
I want to create as many divs as var foo.length, but my code only
I need to count how many of an object there are and then store
Many times I use 'mqsc' for create MQ queue manager from script files but
i need create an email list sending to many emails. what is best solution
I know that horizontal partitioning...you can create many tables. How can you do this
I'm trying to decide whether to create many classes for each content type I
as the title says, I would like to create a many-to-one relationship using Fluent
I am trying to create a form with many groups containing many radio buttons.
I need to create a form that has many of the same fields, that

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.