Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python chat : delete variables to clean memory in functions?

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")
like image 697
plehoux Avatar asked Sep 25 '09 15:09

plehoux


3 Answers

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.

like image 67
J S Avatar answered Oct 17 '22 14:10

J S


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).

like image 7
Kenan Banks Avatar answered Oct 17 '22 16:10

Kenan Banks


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.

like image 4
Georg Schölly Avatar answered Oct 17 '22 15:10

Georg Schölly