Skip to content

Commit fc0b32d

Browse files
Add documentation on how clearing Python proto fields does not reclaim memory.
Add documentation on how clearing Python proto fields does not reclaim memory. For those unfamiliar with this behavior, debugging memory creep as a result of it can be time consuming. Adding some documentation to help them more easily find the issue. PiperOrigin-RevId: 888653180
1 parent ddcfd3e commit fc0b32d

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

content/reference/python/python-generated.md

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -723,6 +723,31 @@ possible message creation more explicit:
723723
m.message_map.get_or_create(10)
724724
```
725725

726+
### Memory management {#clearing-fields}
727+
728+
Note that clearing a field (via `ClearField`, `Clear`, `del` (for lists) or
729+
`ClearExtension`) doesn't necessarily reclaim memory. It just resets the object
730+
to look as if things are unset, but the subobjects *may* continue to exist. For
731+
example, clearing a repeated field sets its size to 0, but does not relinquish
732+
its capacity (analogous to calling `.clear()` on a `std::vector` in C++).
733+
734+
There are various ways in which to ensure the memory is freed up. One method is
735+
to make a copy of the proto after clearing the fields, as the unused memory is
736+
not copied over:
737+
738+
```python
739+
# Assuming 'my_proto' has a memory-intensive field 'big_field'
740+
# Clears the field. Memory might still be reserved.
741+
my_proto.ClearField('big_field')
742+
743+
# Copy to a new instance. This allocates only necessary memory.
744+
compact_proto = my_proto_pb2.MyProto()
745+
compact_proto.CopyFrom(my_proto)
746+
747+
# Once all references to the initial proto are gone, it is garbage collected.
748+
my_proto = compact_proto
749+
```
750+
726751
## Enumerations {#enum}
727752

728753
In Python, enums are just integers. A set of integral constants are defined

0 commit comments

Comments
 (0)