1

目前在 centos 7.4 上运行的 python 2.7 上的 scapy 2.4.0 内存使用率很高

最初我认为这是由于https://bitbucket.org/secdev/scapy/issues/5149/rdpcap-and-wrpcap-file-descriptor-leak造成的,但这已在不久前修复。

import scapy
from guppy import hpy
import psutil
hp = hpy()


class SessionBuilder(object):
    def __init__(self):
        pass

    def get_sessions(pcap):
        # this heap always reports 50mb usage
        hp.heap()
        process = psutil.Process(os.getpid())
        # I expect this memory to be around 50mb... but that's not always true
        print process.memory_info()
        opened_pcap = rdpcap("pcap_location")
        sessions = opened_pcap.session()
        # this heap always reports 50mb usage
        hp.heap()
        # I expect this memory to be larger which is it
        print process.memory_info()
        return sessions

在另一个 python 文件中

import SessionBuilder
class session_worker
    def __init__(self):
        self.sb = SessionBuilder()

    def work(self):
        for pcap in pcaps:
            sessions = self.sb.get_sessions(pcap)


        # I then go about doing some things with these sessions

为代码中的一些错误道歉,但它在离线系统上,所以只是添加了我正在做的粗略部分。

每次我绕过循环时,上一个循环的内存都有可能保留下来,这只会复合,直到我没有剩下的内存(上面的代码中没有提到,但是有一些逻辑可以忽略任何 pcap 是大于盒子上可用内存的 1/4,以确保 scapy 可以打开它并提取会话)

根据Using heapy to track down memory leaks in Django app,堆数量代表了python正在使用的内存,而不是任何底层c代码。我假设scapy在下面使用c?

4

0 回答 0