Skip to main content

Python package for garbage collection utilities and memory management

Project description

gc

Python package for garbage collection utilities and memory management.

Overview

The gc package provides comprehensive tools and utilities for working with Python's garbage collector, memory profiling, and cleanup operations. It offers enhanced garbage collection control, memory monitoring, and debugging capabilities.

Features

  • Enhanced Garbage Collection: Control and monitor Python's garbage collector with detailed statistics
  • Memory Profiling: Track memory usage over time and analyze memory patterns
  • Object Tracking: Monitor specific objects using weak references
  • Reference Cycle Detection: Find and analyze reference cycles in your code
  • Memory Analysis: Comprehensive memory usage analysis and reporting
  • Utility Functions: Common garbage collection and memory management tasks

Installation

pip install gc

Development Installation

git clone https://github.com/tom-sapletta/gc.git
cd gc
pip install -e ".[dev]"

Quick Start

Basic Garbage Collection Control

from gc import GarbageCollector

# Create a garbage collector instance
gc_manager = GarbageCollector()

# Force garbage collection
collected = gc_manager.collect()
print(f"Collected {collected} objects")

# Get memory summary
summary = gc_manager.get_memory_summary()
print(summary)

Memory Profiling

from gc import MemoryProfiler

# Create a profiler instance
profiler = MemoryProfiler()

# Take a memory snapshot
profiler.take_snapshot("before_operation")

# Your code here...
data = [list(range(1000)) for _ in range(100)]

# Take another snapshot
profiler.take_snapshot("after_operation")

# Compare snapshots
comparison = profiler.compare_snapshots(0, 1)
print(f"Memory change: {comparison['rss_diff']} bytes")

Memory Monitoring

from gc.utils import monitor_memory_usage

# Monitor memory for 60 seconds
samples = monitor_memory_usage(duration=60, interval=1.0)

for sample in samples:
    print(f"Memory: {sample['rss']} bytes, Objects: {sample['objects_count']}")

API Reference

GarbageCollector

Main class for garbage collection control and monitoring.

Methods

  • enable() - Enable garbage collection
  • disable() - Disable garbage collection
  • collect(generation=2) - Force garbage collection
  • get_stats() - Get garbage collection statistics
  • get_memory_summary() - Get comprehensive memory summary

MemoryProfiler

Class for memory profiling and object tracking.

Methods

  • take_snapshot(label="") - Take a memory snapshot
  • track_object(obj, label="") - Track an object with weak reference
  • compare_snapshots(index1, index2) - Compare two memory snapshots
  • get_tracked_objects() - Get information about tracked objects

Utility Functions

  • cleanup_temp_files(pattern="*") - Clean up temporary files
  • monitor_memory_usage(duration=60, interval=1.0) - Monitor memory usage
  • force_garbage_collection(verbose=False) - Force garbage collection on all generations
  • find_object_cycles(obj, max_depth=10) - Find reference cycles
  • analyze_memory_usage() - Comprehensive memory analysis

Requirements

  • Python 3.8+
  • psutil>=5.8.0

Development

Running Tests

pytest

Code Formatting

black gc/

Type Checking

mypy gc/

License

Apache License 2.0 - see LICENSE for details.

Contributing

Contributions are welcome! Please read the CONTRIBUTING.md file for details on our code of conduct and the process for submitting pull requests.

Changelog

0.1.0

  • Initial release
  • Basic garbage collection control
  • Memory profiling capabilities
  • Utility functions for memory management

License

Apache License 2.0 - see LICENSE for details.

Author

Created by Tom Sapletta - tom@sapletta.com

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

glon-0.1.9.tar.gz (18.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

glon-0.1.9-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file glon-0.1.9.tar.gz.

File metadata

  • Download URL: glon-0.1.9.tar.gz
  • Upload date:
  • Size: 18.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for glon-0.1.9.tar.gz
Algorithm Hash digest
SHA256 319de306175af3765f9a84e587081cff67e2cf8a225429a9fef24f00a6ffff1d
MD5 99ac2dbf05abffa59375fa4dd1ff89fc
BLAKE2b-256 43381f32a9922486ff72d4d2e39d100935107235f0f782b08cb5d92f549a8014

See more details on using hashes here.

File details

Details for the file glon-0.1.9-py3-none-any.whl.

File metadata

  • Download URL: glon-0.1.9-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.7

File hashes

Hashes for glon-0.1.9-py3-none-any.whl
Algorithm Hash digest
SHA256 0cd35fd56052b2115086f86dd43759e2c726b9b1e2d58da82b46c1f48848a9cd
MD5 5e0359fb0d28bda6a64d98cf1cb10d7a
BLAKE2b-256 98fac90c189e5c6a7a15eed416f034f1fe7e2b2fa89f3ef4eb97c2f094e965b0

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page