The ClassTracker is a facility delivering insight into the memory distribution of a Python program. It can introspect memory consumption of certain classes and objects. Facilities are provided to track and size individual objects or all instances of certain classes. Tracked objects are sized recursively to provide an overview of memory distribution between the different tracked objects.
Track object ‘instance’ and sample size and lifetime information. Not all objects can be tracked; trackable objects are class instances and other objects that can be weakly referenced. When an object cannot be tracked, a TypeError is raised.
Parameters: |
|
---|
Track all objects of the class cls. Objects of that type that already exist are not tracked. If track_class is called for a class already tracked, the tracking parameters are modified. Instantiation traces can be generated by setting trace to True. A constructor is injected to begin instance tracking on creation of the object. The constructor calls track_object internally.
Parameters: |
|
---|
Stop tracking class ‘cls’. Any new objects of that type are not tracked anymore. Existing objects are still tracked.
Detach from all tracked classes.
Detach from all tracked classes and objects. Restore the original constructors and cleanse the tracking lists.
Clear all gathered data and detach from all tracked objects/classes.
Start a thread which takes snapshots periodically. The interval specifies the time in seconds the thread waits between taking snapshots. The thread is started as a daemon allowing the program to exit. If periodic snapshots are already active, the interval is updated.
Post a stop signal to the thread that takes the periodic snapshots. The function waits for the thread to terminate which can take some time depending on the configured interval.
Collect current per instance statistics and saves total amount of memory associated with the Python process.
If compute_total is True, the total consumption of all objects known to asizeof is computed. The latter might be very slow if many objects are mapped into memory at the time the snapshot is taken. Therefore, compute_total is set to False by default.
The overhead of the ClassTracker structure is also computed.
Snapshots can be taken asynchronously. The function is protected with a lock to prevent race conditions.