Analyze and simulate dynamic memory usage in C/C++ code
Project description
memscope
🔍 Analyze and visualize memory allocation and release operations in C/C++ code.
🚀 Features
- Detects
malloc,calloc,realloc,new,delete,free - Counts allocation vs. deallocation operations
- Detects potential memory leaks
- Visualizes heap behavior over time using Matplotlib
📦 Installation
pip install memscope
Or from source:
bash
Copy code
git clone https://github.com/YOUR_USER/memscope
cd memscope
pip install -e .
🧠 Quick Start
Analyze C/C++ Source Code
python
Copy code
from memscope import analyze_source
result = analyze_source("examples/sample.c")
print(result)
Example output:
python
Copy code
{
"allocs": 2,
"frees": 1,
"unfreed_allocations": 1,
"lines": {
"alloc": [4, 7],
"free": [9]
}
}
Visualize Memory Operations
python
Copy code
from memscope.visualizer import plot_memory_timeline, plot_memory_bars
events = [
{"step": 1, "allocated": 128, "freed": 0},
{"step": 2, "allocated": 256, "freed": 64},
{"step": 3, "allocated": 0, "freed": 320},
]
plot_memory_timeline(events)
plot_memory_bars(events)
🧩 API Reference
analyze_source(filepath: str) -> dict
Analyze a C/C++ source file and detect allocation/deallocation operations.
Parameters
filepath: path to .c or .cpp file.
Returns
python
Copy code
{
"allocs": int,
"frees": int,
"unfreed_allocations": int,
"lines": {"alloc": list[int], "free": list[int]}
}
plot_memory_timeline(events: list[dict])
Plots total heap size over time.
python
Copy code
plot_memory_timeline([
{"step": 1, "allocated": 128, "freed": 0},
{"step": 2, "allocated": 0, "freed": 128},
])
plot_memory_bars(events: list[dict])
Plots allocation (green) and free (red) deltas.
python
Copy code
plot_memory_bars([
{"step": 1, "allocated": 200, "freed": 0},
{"step": 2, "allocated": 0, "freed": 100},
])
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
memscope-0.1.1.tar.gz
(4.0 kB
view details)
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 memscope-0.1.1.tar.gz.
File metadata
- Download URL: memscope-0.1.1.tar.gz
- Upload date:
- Size: 4.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7b461b6ab688e5cb20a44be1fc9e2b164ddd681e25cdced479e9a240fbadcda0
|
|
| MD5 |
a8af7157b8924a9ef97006286b30b657
|
|
| BLAKE2b-256 |
376272fa52387048d6d5263e1483cf08d944b9e1d820958493fa694abc3d0dbf
|
File details
Details for the file memscope-0.1.1-py3-none-any.whl.
File metadata
- Download URL: memscope-0.1.1-py3-none-any.whl
- Upload date:
- Size: 4.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.11.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ae297e796007eb1b16220d3fd38ee40a2663cb779bfabd0df53edd7db0ca9eb0
|
|
| MD5 |
5a9939278b06fae18056141bad77b799
|
|
| BLAKE2b-256 |
086a6606107c511c84e04eb344b74698c785dfc327dd8711525f28eac4d454ef
|