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