[Twisted-Python] Memory Usage Problem

Hi All, I have a problem which is that memory usage of twisted is increasing but not decreasing over time. After few days, it reaches 520MB. You may find the codes below; How may I reduce the usage of memory? Twisted version: 8.2.0 Python version: 2.4.3 [root@labris init.d]# cat /usr/bin/labris-flyng.tac #!/usr/bin/python import os from twisted.application import internet, service from twisted.internet import reactor from twisted.enterprise import adbapi from twisted.plugin import getPlugins from twisted.python import log import labris.flyng.iflyng as iflyng import labris.flyng.config as config import labris.flyng.plugins as pplugins import labris.flyng.protocols as flyng_protocols from labris.flyng.config import flyng_config # Database type to package dictionary DB_PACKAGE_DICT = { "postgresql" : "psycopg2" } def initDatabase(config): """ Create database connection pool """ dbtype = config.getDBType() dbpackage = DB_PACKAGE_DICT.get(dbtype, None) if not dbpackage: print "Database type % is not supported yet!" %(dbtype) sys.exit(1) if dbtype == "mysql": dbpool = adbapi.ConnectionPool( dbpackage, config.getDBHost(), config.getDBUser(), config.getDBPassword(), config.getDBName() ) else: dbpool = adbapi.ConnectionPool( dbpackage, database=config.getDBName(), user=config.getDBUser(), password=config.getDBPassword(), host=config.getDBHost() ) dbpool.connect() return dbpool def setupPlugins(config, dbpool): """ Initialize and setup plugins """ for plugin in getPlugins(iflyng.IFlyngPlugin, pplugins): print "* Plugin %s" %(plugin.name) if not config.isEnabled(plugin.name): print " - Not enabled" continue plugin.init(config, dbpool) for type, port in config.getListeningPorts(plugin.name): port = port.replace('"', "") if type == "tcp": print " -> TCP port: %s" %(port) factory = flyng_protocols.FlyngServerFactory(plugin) internet.TCPServer( int(port), factory ).setServiceParent(application) elif type == "udp": print " -> UDP port: %s" %(port) internet.UDPServer( int(port), flyng_protocols.FlyngUDPProtocol(plugin) ).setServiceParent(application) elif type == "unix": print " -> UNIX stream: %s" %(port) if os.path.exists(port): os.unlink(port) factory = flyng_protocols.FlyngServerFactory(plugin) internet.UNIXServer( port, factory ).setServiceParent(application) else: raise "connection type %s is not supported" %(port) #log.startLogging(open("/var/log/labris-flyng.log", "a")) config = config.Configuration() uid, gid = config.getRunAs() application = service.Application('labris-flyng', uid, gid) dbpool = initDatabase(config) setupPlugins(config, dbpool) ------------------------------------------------------------------- [root@labris init.d]# cat /usr/bin/labris-pyng.tac #!/usr/bin/python """ A syslog-ng to database mapping service """ import os from twisted.internet import protocol, reactor, defer from twisted.application import internet, service from twisted.python import log from labris.logutils import factories from labris.logutils.config import pyng_config class LogFactory(protocol.ServerFactory): pass class UnixLogFactory(protocol.ServerFactory): pass def openUNIXStreams(configuration, application): for logtype, stream in configuration.getUnixStreams(): try: if not pyng_config.getboolean(logtype.strip(), "enabled"): print "Warning: UNIX stream: ", logtype, " is DISABLED" continue print "\nPlugin: %s [UNIX-Stream(%s)]" %(logtype, stream) factory = UnixLogFactory() factory.protocol = factories.ProtocolFactory.getLogProtocol(logtype.strip(), "stream") # delete old stream if os.path.exists(stream): os.unlink(stream) internet.UNIXServer(stream.strip(), factory).setServiceParent(application) except factories.NoSuchProtocolException, e: print "Error: no such protocol:", e.getProtoname() for logtype, sock in configuration.getUnixDgrams(): try: if not pyng_config.getboolean(logtype.strip(), "enabled"): print "Warning: UNIX dgram: ", logtype, " is DISABLED" continue print "\nPlugin: %s [UNIX-Datagram(%s)]" %(logtype, stream) protocol = factories.ProtocolFactory.getLogProtocol(logtype.strip(), "datagram") # delete old stream if os.path.exists(sock): os.unlink(sock) internet.UNIXDatagramServer(sock.strip(), protocol()).setServiceParent(application) except factories.NoSuchProtocolException, e: print "Error: no such protocol:", e.getProtoname() def openSockets(configuration, application): for logtype, hostname, port in configuration.getTCP(): try: if not pyng_config.getboolean(logtype.strip(), "enabled"): print "Warning: TCP socket: ", logtype, " is DISABLED" continue print "\nPlugin: %s [TCP => port:%d]" %(logtype, int(port)) factory = LogFactory() factory.protocol = factories.ProtocolFactory.getLogProtocol(logtype.strip(), "stream") internet.TCPServer(int(port), factory).setServiceParent(application) except factories.NoSuchProtocolException, e: print "Error: no such protocol:", e.getProtoname() for logtype, hostname, port in configuration.getUDP(): try: if not pyng_config.getboolean(logtype.strip(), "enabled"): print "Warning: UDP socket: ", logtype, " is DISABLED" continue print "\nPlugin: %s [UDP => port:%d]" %(logtype, int(port)) protocol = factories.ProtocolFactory.getLogProtocol(logtype.strip(), "datagram") internet.UDPServer(int(port), protocol()).setServiceParent(application) except factories.NoSuchProtocolException, e: print "Error: no such protocol:", e.getProtoname() #log.startLogging(open("/var/log/labris-pyng.log", "a")) from labris.logutils.dao.pyngdb import PyNgDB PyNgDB.initDatabase(pyng_config.bind()) application = service.Application('labris-pyng', pyng_config.uid(), pyng_config.gid()) openUNIXStreams(pyng_config, application) openSockets(pyng_config, application)

Le lundi 06 septembre 2010 à 08:28 +0000, Ozgur Pekcagliyan a écrit :
Hi All,
I have a problem which is that memory usage of twisted is increasing but not decreasing over time.
After few days, it reaches 520MB. You may find the codes below; How may I reduce the usage of memory? Twisted version: 8.2.0 Python version: 2.4.3
[snip] It's a bit hard to tell, because you're only showing the tac files, and not the application code, where the memory leak is likely to be. But: * Twisted 8.2 is fairly old, it would worth to try a more recent version. * Python 2.4.3 is very old too, *and* contains a pretty bad bug in the socket module (unless your platform patched). Note also that only Python 2.5 started releasing memory (IIRC). -- Thomas

I've tried to upgrade twisted version 10.1.0. But problem still exists. I will try to upgrade python but still I have doubts that will fix my problem. Any Idea?

On Sep 6, 2010, at 7:31 AM, Ozgur Pekcagliyan wrote:
I've tried to upgrade twisted version 10.1.0. But problem still exists. I will try to upgrade python but still I have doubts that will fix my problem. Any Idea?
Try debugging with a memory profiling tool like Heapy: <http://guppy-pe.sourceforge.net/#Heapy>. My first guess is that this is simply a bug in your application, where you're leaking some objects, and may have nothing to do with Twisted at all. Of course, it could also be a bug in Twisted, but if it is, you'll need to tell us what it is before we can fix it. Since the first Google hit I get for "labris Python" is your first mailing list post, there's no way to try your code, and even if there were, it looks like a big complex program that I don't understand. If you can reduce your memory leak to a short, self-contained, correct example, (<http://sscce.org/>) then we might be able to do something about it. Good luck!
participants (3)
-
Glyph Lefkowitz
-
Ozgur Pekcagliyan
-
Thomas Hervé