-
Notifications
You must be signed in to change notification settings - Fork 422
Add Python object reference tracking #752
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
32d2317
to
d748e4d
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## main #752 +/- ##
==========================================
- Coverage 92.01% 90.66% -1.36%
==========================================
Files 95 98 +3
Lines 11940 12383 +443
Branches 413 420 +7
==========================================
+ Hits 10987 11227 +240
- Misses 953 1156 +203
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
Tests require 3.13.3 at least because before there is a bug that causes some objects to be identified wrongly. cibuildwheel only has this when pypa/cibuildwheel#2359 passes |
6b081eb
to
a03e9de
Compare
I just released cibuildwheel with 3.13.3 support! |
227cf37
to
9de0d3b
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lots of comments, mostly pretty small things. The overall approach looks good to me. I have some concerns about some of the names, and how we handle the uninstallation of the hooks (and maybe even the installation?) in the free-threading world.
|
||
# THEN | ||
# No surviving objects should be recorded when reference_tracking is disabled | ||
assert len(tracker.get_surviving_objects()) == 0 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should probably raise an exception if this is called when reference tracking was never enabled. It seems like user error...
Also, this test doesn't prove what it says it does. It would pass even if reference tracking were enabled, because no objects leak (thanks to the del
and gc.collect()
calls)
9b3c024
to
263bedf
Compare
This feature adds the capability to track Python objects throughout their lifecycle in memray. The implementation introduces a reference tracking system that records PyObject creation and destruction events, storing them as TrackedObject records in the memray capture file with associated stack traces. The Tracker class now accepts a reference_tracking flag to enable this functionality and provides a get_surviving_objects() method to retrieve live objects at the end of tracking. Object tracking works across threads and supports native, Python, and hybrid stack traces, providing insights into Python object lifetimes. This enhancement extends memray beyond memory allocation tracking to give developers a powerful tool for understanding object lifecycles and identifying potential reference leaks. With this addition, memray can now correlate memory allocations with the Python objects that trigger them, enabling more precise debugging of memory usage patterns. The feature is particularly useful for identifying objects that persist longer than expected or detecting unexpected object creation patterns. By tracking both memory allocations and object references, memray provides a more complete picture of a program's memory behavior, helping developers optimize memory usage and resolve leaks more effectively. This implementation leverages the new Python reference tracing API that was introduced in CPython 3.13. Signed-off-by: Pablo Galindo <[email protected]>
This feature adds the capability to track Python objects throughout
their lifecycle in memray. The implementation introduces a reference
tracking system that records PyObject creation and destruction events,
storing them as TrackedObject records in the memray capture file with
associated stack traces. The Tracker class now accepts a
reference_tracking flag to enable this functionality and provides a
get_surviving_objects() method to retrieve live objects at the end of
tracking.
Object tracking works across threads and supports native, Python, and
hybrid stack traces, providing insights into Python object
lifetimes. This enhancement extends memray beyond memory allocation
tracking to give developers a powerful tool for understanding object
lifecycles and identifying potential reference leaks.
With this addition, memray can now correlate memory allocations with the
Python objects that trigger them, enabling more precise debugging of
memory usage patterns. The feature is particularly useful for
identifying objects that persist longer than expected or detecting
unexpected object creation patterns. By tracking both memory allocations
and object references, memray provides a more complete picture of a
program's memory behavior, helping developers optimize memory usage and
resolve leaks more effectively.
This implementation leverages the new Python reference tracing API that
was introduced in CPython 3.13.
Signed-off-by: Pablo Galindo [email protected]
Issue number of the reported bug or feature request: #
Describe your changes
A clear and concise description of the changes you have made.
Testing performed
Describe the testing you have performed to ensure that the bug has been addressed, or that the new feature works as planned.
Additional context
Add any other context about your contribution here.