Python chat : delete variables to clean memory in functions?

34,980

Solution 1

C Python (the reference implementation) uses reference counting and garbage collection. When count of references to object decrease to 0, it is automatically reclaimed. The garbage collection normally reclaims only those objects that refer to each other (or other objects from them) and thus cannot be reclaimed by reference counting.

Thus, in most cases, local variables are reclaimed at the end of the function, because at the exit from the function, the objects cease being referenced from anywhere. So your "del" statements are completely unnecessary, because Python does that anyway.

Solution 2

Python objects are never explicitly deleted. The only way to truly reclaim memory from unreferenced Python objects is via the garbage collector. The del keyword simply unbinds a name from an object, but the object still needs to be garbage collected.

If you really think you have to, you can force the garbage collector to run using the gc module, but this is almost certainly a premature optimization, and you are quite likely to garbage collect at inopportune times or otherwise inefficiently unless you really know what you're doing.

Using del as you have above has no real effect, since those names would have been deleted as they went out of scope anyway. You would need to follow up with an explicit garbage collection to be sure(r).

Solution 3

Python uses garbage collection. This means you don't have to care about memory as it's freed automatically when it's not used anymore.

Share:
34,980
plehoux
Author by

plehoux

Updated on September 26, 2020

Comments

  • plehoux
    plehoux over 3 years

    I'm creating a chat daemon in python and twisted framework. And I'm wondering if I have to delete every variable create in my functions to save memory in the long run when multiple users are connected, or are those variable automatically clear?. Here's a strip down version of my code to illustrate my point:

    class Chat(LineOnlyReceiver):
    
        LineOnlyReceiver.MAX_LENGTH = 500
    
        def lineReceived(self, data):
    
                self.sendMessage(data)
    
        def sendMessage(self, data):
    
                try:
                    message = data.split(None,1)[1]
                except IndexError:
                    return
    
                self.factory.sendAll(message)
    
                #QUESTION : do i have to delete message and date??????????????????
    
                del message
                del data
    
    
    class ChatFactory(Factory):
        protocol = Chat
    
        def __init__(self):
            self.clients = []
    
        def addClient(self, newclient):
            self.clients.append(newclient)
    
        def delClient(self, client):
            self.clients.remove(client)
    
        def sendAll(self, message):
            for client in self.clients:
                client.transport.write(message + "\n")
    
  • Kenan Banks
    Kenan Banks over 14 years
    Python garbage collection is technically completely optional, is implementation dependent, and is not guaranteed to ever happen, especially with circular references.
  • Stefan
    Stefan over 14 years
    Chances are good that CPython is going to improve its garbage collector to something that can deal with circular references.
  • J S
    J S over 14 years
    gs: CPython garbage collector can deal with circular references for a long long time, since version about 2.0.
  • Stefan
    Stefan over 14 years
    @ J S: Thanks, didn't know that.