A lightweight and colorful Python memory allocation tracking tool built on top of tracemalloc.
Project description
memlog
A lightweight and colorful Python memory allocation tracking tool built on top of tracemalloc.
memlog helps you monitor memory usage, identify potential memory leaks, and compare memory snapshots during function execution or within a specific context. It provides clear, formatted table output in your terminal.
Features
- Stop/Clear Support: Support for stopping and clearing traces.
- Environment-controlled activation: Enable tracking only when needed using an environment variable.
- Synchronous & Asynchronous Support: Works seamlessly with both standard and
asyncfunctions. - Flexible Usage: Use as a decorator, context manager, or manual API.
- Filtering Capabilities: Include specific file paths or modules using
filters(supports string list ortracemalloc.Filter). - Colorful Output: Displays memory statistics and differences in a clean, readable table format (automatically detects TTY for color).
- Comparison Modes: Compare current memory against a baseline (first snapshot) or the start of a specific block.
Installation
pip install memlog
Usage
1. Using as a Decorator
Easily track memory usage of a specific function.
import memlog
# Start tracking
memlog.start()
@memlog.snapshot(title="Data Processing", top_k=5)
def process_data():
# Your memory-intensive code here
data = [i for i in range(1000000)]
return len(data)
# When called, it will print a comparison table showing memory changes
process_data()
The decorator also supports async functions:
@memlog.snapshot(title="Async Task")
async def run_task():
await asyncio.sleep(1)
2. Using as a Context Manager
Track memory within a specific block of code.
import memlog
# Start tracking
memlog.start()
with memlog.snapshot_manager(title="Block Comparison", top_k=3):
temp_list = [str(i) for i in range(50000)]
# Comparison table is printed when exiting the block
3. Manual Snapshotting
For more granular control, use the manual API.
import memlog
# Start tracking (automatically called if MEMLOG_ENABLE=1)
memlog.start()
# Take an initial snapshot
s1 = memlog.take_snapshot("Initial State")
# ... run some code ...
# Take another snapshot and compare
s2 = memlog.take_snapshot("After Operation")
s2.compare_to(s1).show(top_k=10)
API Reference
Core Functions
memlog.start(): Initializestracemallocand records the "First Snapshot".memlog.stop(): Stopstracemallocand clears snapshots.memlog.clear(): Clearstracemalloctraces and snapshots.memlog.take_snapshot(title=None, filters=None): Captures the current memory state.filters: A list of strings ortracemalloc.Filterobjects to include in the traceback (e.g.,["src/memlog"]).
memlog.get_first_snapshot(): Returns the very first snapshot taken whenmemlogwas started.memlog.snapshot(mode='start', title=None, filters=None, top_k=10, key_type=KeyType.TRACEBACK): A decorator for functions.mode='start': Compares the end state to the state just before the function started.mode='first': Compares the end state to the global "First Snapshot".
memlog.snapshot_manager(mode='start', title=None, filters=None, top_k=10, key_type=KeyType.TRACEBACK): A context manager.
Snapshot Methods
snapshot.compare_to(other_snapshot, key_type=KeyType.TRACEBACK, cumulative=False): Returns a comparison object between two snapshots.snapshot.compare(key_type=KeyType.TRACEBACK, cumulative=False): Compares current snapshot to the global "First Snapshot".snapshot.statistics(key_type=KeyType.TRACEBACK, cumulative=False): Returns statistics for the snapshot.snapshot.dump(filename): Save snapshot to a file.snapshot.load(filename): Load snapshot from a file.
Statistics Methods
statistics.show(top_k=10): Prints the formatted statistics to stdout.statistics.show_table(top_k=10): Prints a formatted table to stdout.
License
This project is licensed under the MIT License - see the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file memlog-0.1.0.tar.gz.
File metadata
- Download URL: memlog-0.1.0.tar.gz
- Upload date:
- Size: 7.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
20166bad004e6f3ef284eecd6ee0346dfd3309d8e9ed3da30f5c0eefde6fcb75
|
|
| MD5 |
862834ddfca0c87dd93956010f3c7475
|
|
| BLAKE2b-256 |
5b459f1b9b098070ed11a22d190c90c41ba296fd3ce92a6422422810c2540bc3
|
File details
Details for the file memlog-0.1.0-py3-none-any.whl.
File metadata
- Download URL: memlog-0.1.0-py3-none-any.whl
- Upload date:
- Size: 8.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.18
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66f0832e7df0b9610ccab2638eaf8c019344a7cf7bf8d3222fd79cfd2944e5ec
|
|
| MD5 |
6394cec42f3b9c0c8016b0e47739a3e3
|
|
| BLAKE2b-256 |
4b457d217ee8faa1d5a2cc64dc9190946e044d21006f16acffdbb0999a60cdf0
|