Solution to memory leakage caused by misuse of the logging module in Python

Source: Internet
Author: User

Solution to memory leakage caused by misuse of the logging module in Python

Solution to memory leakage caused by misuse of the logging module in Python

This article mainly introduces how to solve the memory leakage caused by misuse of the logging module in Python. For problems caused by excessive UDP connections, refer

First, let's introduce how to find it. The online project log is sent to syslog through the logging module. After a while, we found that the syslog UDP connection exceeded 8 W, which is 8 W. this is mainly because the logging module is not used properly.

We previously had such a requirement that the current connection information is output for each connection log. Therefore, a log instance is created for each connection and a Formatter is assigned, to distinguish other connections, I simply and roughly used the id of the current object as the log Name:

?

1

2

3

4

5

6

7

Import logging

 

 

Class Connection (object ):

Def _ init _ (self ):

Self. _ logger_name = "Connection. {}". format (id (self ))

Self. logger = logging. getLogger (self. _ logger_name)

Of course, in the test environment, DEBUG is enabled, so DEBUG won't be used in syslog, so there won't be too many UDP connections, and there will be no memory leakage, let's take a look at the cause of Memory leakage. First, let's look at the getLogger code:

?

1

2

3

4

5

6

7

8

9

10

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

Logger. manager. getLogger is called. This function contains the following code snippet:

?

1

2

3

4

5

6

7

8

9

10

11

12

13

14

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)

To ensure that the same name references the same log instance, the logging module stores all log instances in a loggerDict dictionary, the created log instance reference will not be released, so the handlers in the log instance will not be released. previously, I used the Object id as a part of the log name, so the UDP connection created by SyslogHandler has been occupied, resulting in too many UDP connections.

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

?

1

2

3

Logging. Logger. manager. loggerDict. pop (self. _ logger_name)

Self. logger. manager = None

Self. logger. handlers = []

It is said that only the first line of code above should be released, but no, so with the third line of code, SyslogHandler will be released. This problem is unknown for the time being, check again.

Update if the log name is. the logging module uses the last part as the log name and looks up for the parent Logger. If the parent Logger cannot be found, the PlaceHolder object is created as the parent and the Logger is referenced.

For example, the created Logger is named. b. c, then the actual name is c, and B is the parent of c, a is the parent of B, if there is no Logger with this name, The PlaceHolder object is created as a replacement, placeHolder creates a reference to the current Logger. therefore, the name of the log object to be recycled should not contain.

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.