protocolbuffers/protobuf

[Python] Potential memory leak

Aperta

#24.257 aperta il 3 nov 2025

 (3 commenti) (0 reazioni) (0 assegnatari)C++ (16.128 fork)batch import
help wantedpython

Metriche repository

Star
 (71.223 stelle)
Metriche merge PR
 (Merge medio 2g 11h) (185 PR mergiate in 30 g)

Descrizione

What version of protobuf and what language are you using? Version: 6.33.0 Language: Python

What operating system (Linux, Windows, ...) and version? OS: Ubuntu Linux 22.0.1 Kernel: 6.8.0-87-generic

What runtime / compiler are you using (e.g., python version or gcc version) Python 3.12

What did you do? I've hacked together a little example repo (kudos to @Atheuz for the original that I've forked from), but essentially it is as follows:

obj = schema_pb2.SomeObj()

for _ in range(10_000_000):
    obj.ParseFromString(b"...")

del obj

What did you expect to see

That there would be no growth in memory usage

What did you see instead?

Until the obj is deleted, the memory grew uncontrollably.

Anything else we should know about your project / environment

This is a continuation of https://github.com/protocolbuffers/protobuf/issues/10088 (I think).

I've started a Google Group Discussion where it was explained to me, that the behaviour is intended and expected.

But it is still kind of counterintuitive to see that Unpack and ParseFromString actually allocate more and more memory. I think it deserves at least a mention in the docs on how to properly use them. And thus I was advised to open an issue here.

Thank you!

Guida contributor