Resolves memory leaks caused by misuse of logging modules in Python _python

Source: Internet
Author: User
Tags syslog in python

First introduced how to find it, the online project log is through the logging module to the Syslog, ran a period of time after the discovery of the syslog UDP connection over 8W, yes is 8 W. The main thing is that the logging module is used incorrectly.

We had such a requirement, that is, for each connection log output the current connection information, so each connection creates a log instance, and assigns a Formatter, creates the log instance in order to differentiate the other connections so I'm simply rude. Use the ID of the current object as the journal name:

Import Logging


class Connection (object):
  def __init__ (self):
    self._logger_name = "connection.{}". Format (ID (self))
    Self.logger = Logging.getlogger (self._logger_name)

Of course, the test environment is open debug, debug will not go to the syslog, so there will not be too many UDP connections, you will not know that there are memory leaks, let's see why this leads to memory leaks, first look at the GetLogger code:

def getlogger (Name=none): "" "Return
  a logger with the specified name, creating it if necessary.

  If No name is specified, return the root logger.
  "" " If Name: Return
    Logger.manager.getLogger (name)
  else: return
    root

The main call to Logger.manager.getLogger, this function has the following section of code fragment

      If name in self.loggerdict:
        RV = self.loggerdict[name]
        if Isinstance (rv, placeholder):
          ph = RV
          RV = (self . Loggerclass or _loggerclass) (name)
          Rv.manager = self
          self.loggerdict[name] = RV
          self._fixupchildren (ph, RV)
          self._fixupparents (RV)
      else:
        RV = (Self.loggerclass or _loggerclass) (name)
        Rv.manager = self
        Self.loggerdict[name] = RV
        self._fixupparents (RV)

Logging module to ensure that the same name refers to the same log instance, so that all the log instances exist in a loggerdict dictionary, so unless the program exits, the creation of the log instance reference is not released, so the log instance of the handlers will not be released. The ID of the object I used before was part of the log name, so the UDP connection created by Sysloghandler has been occupied and caused too many UDP connections.

To solve this problem I added the following code when the connection was closed:

Logging. Logger.manager.loggerDict.pop (self._logger_name)
Self.logger.manager = None
self.logger.handlers = []

Supposedly only add the first line of code should be released, but no, so there is a third line of code, Sysloghandler finally released, the issue is not yet known why, but also need to check.

2015-03-30 Update if the journal name is with. Delimited, the logging module takes the last part as the log name and goes up to look for the parent Logger, and if not found, creates the placeholder object as the parent and references Logger.

For example, the Logger name created is called A.B.C, then the actual name is C, and B is the parent of C, and A is the parent of B, and if a Logger without that name creates a placeholder object instead, placeholder creates the current A reference to the Logger. So the name of the log object that needs to be reclaimed should not be included.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.