Skip to content

[Python] PyUnknownFields Memory leak #7301

@TimKingNF

Description

@TimKingNF

What version of protobuf and what language are you using?
Version: v3.5.1
Language: Python

What operating system (Linux, Windows, ...) and version?
System: Linux / Mac

What runtime / compiler are you using (e.g., python version or gcc version)
PythonVersion: 3.7.4
ProtobufPackageVersion: 3.11.3

What did you do?

syntax = "proto3";

message Msg
{
  uint32 u32_id = 1;
}
import os, psutil, gc
import tracemalloc

from a_pb2 import Msg

def get_mem():
    rss = psutil.Process(os.getpid()).memory_info().rss
    print(rss / 1024 / 1024)

def get_unknown_field():
    msg = Msg()
    unknown_fields = msg.UnknownFields()  # it raise memory leak
    #  print(unknown_fields)

tracemalloc.start()

get_mem()

for i in range(10000000):
    get_unknown_field()

gc.collect()
get_mem()

snapshot = tracemalloc.take_snapshot()
top_stats = snapshot.statistics('lineno')
for stat in top_stats:
    print(stat)

What did you expect to see
Process's memory will be freed.

What did you see instead?
Python Output:
image

Output with tracemalloc:
image

It looks like the memory has not been freed. May be it's a bug?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions