Skip to content

IPRoute and subprocess increase memory usage #1355

@qpwo2468

Description

@qpwo2468

hello, I want to know why my Python process memory keeps growing, even when I call gc. collect(). It just grew slower and slower.

import os
from pyroute2 import IPRoute
import gc
import sys
import psutil
import subprocess

def add_garbage_collection(phase, info):
  if phase == "start" and info['generation'] == 2:
    count = gc.get_count()
    print(f"gc info = {info}, secound generation count = {count[2]}")

gc.set_threshold(700, 10, 10)
gc.callbacks.append(add_garbage_collection)
process = psutil.Process(os.getpid())
mem0 = process.memory_info().rss


while True:
    with IPRoute() as ipr:
        pass
    ret = subprocess.Popen("ls", shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding='utf-8', env=dict(os.environ))
    out, err = ret.communicate()
    del out, ret, err
    #print(f"gc count = {gc.get_count()}")
    mem1 = process.memory_info().rss
    increase = mem1 - mem0
    if increase > 0:
        print(f"fd num = {len(os.listdir('/proc/self/fd'))}")
        print('Memory Increase After Action', increase,'B')
        print('##############AFTER############################')
    elif increase < 0:
        print('Memory Decrease After Action', increase,'B')

    mem0 = mem1

Image

Pyroute Version: 0.5.18
Python Version: 3.7.9
Linux Version: 4.19.9

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions