Rust Arc GC (rust-arc-gc)
Introduction
rust-arc-gc is a simple garbage collection (GC) implementation library designed for Rust, providing reference counting functionality similar to Rust's standard library Arc, but with added garbage collection capabilities. This library is particularly suitable for handling circular reference problems and applications requiring efficient memory management.
This library combines Rust's memory safety features with the convenience of garbage collection, offering a safe way to manage complex object graphs in Rust.
Core Features
- Garbage Collection: Can detect and release objects that are no longer referenced, including circularly referenced objects
- Heuristic Collection: Automatically triggers garbage collection when attach operations exceed a configurable percentage threshold
- Type Safety: Completely type-safe, no need for
unsafecode (except within the library itself) - Concurrency Safety: All operations are thread-safe with atomic counters
- Weak Reference Support: Provides
GCArcWeaktype for solving circular reference problems - Reference Tracking: Implement the
GCTraceabletrait to make objects part of garbage collection
Usage
Installation
Use cargo add rust-arc-gc to add the library to your project.
API Reference
GC
GC::new()- Create a new garbage collector instance with default 20% heuristic thresholdGC::new_with_percentage(percentage)- Create a garbage collector with custom heuristic threshold (e.g., 30 for 30%)gc.attach(obj)- Add an object to the garbage collector's tracking scope (may trigger automatic collection)gc.collect()- Manually perform garbage collectiongc.object_count()- Return the current number of objects managed by the garbage collectorgc.create(obj)- Create a new object and add it to the garbage collector
Heuristic Collection
The garbage collector uses an atomic counter to track attach operations and automatically triggers collection when the number of attach operations exceeds a configurable percentage of the current object count. This helps maintain memory efficiency without requiring manual collection calls.
- Default threshold: 20% (triggers when attach_count >= current_objects * 0.2)
- Configurable via
new_with_percentage() - Counter resets after each collection cycle
- Thread-safe atomic operations
GCArc
GCArc::new(obj)- Create a new reference-counted objectarc.as_ref()- Get a reference to the objectarc.as_mut()- Get a mutable reference to the objectarc.mark_and_visit()- Mark the object and visit its referenced objectsarc.is_marked()- Check if the object is markedarc.as_weak()- Create a weak reference to the object
GCTraceable
A trait that must be implemented to allow the garbage collector to track objects:
GCArcWeak
GCArcWeak::upgrade()- Upgrade a weak reference to a strong reference, returningNoneif the object has been collectedGCArcWeak::is_valid()- Check if the weak reference is valid (i.e., the object has not been collected)
Performance Considerations
The heuristic collection system is designed to balance memory usage and performance:
- Automatic Management: Reduces the need for manual
collect()calls - Configurable Thresholds: Adjust collection frequency based on application needs
- Atomic Operations: Minimal overhead for tracking attach operations
- Thread Safety: All operations are safe for concurrent use
For applications with predictable allocation patterns, consider adjusting the percentage threshold:
- Lower percentages (10-15%): More frequent collection, lower memory usage
- Higher percentages (30-50%): Less frequent collection, potentially higher memory usage
Limitations and Future Plans
- The current implementation uses a mark-sweep garbage collection algorithm; generational collection may be added in the future
- Performance optimization: reduce pause time during garbage collection
- Add richer debugging tools and memory usage statistics
- Consider adaptive thresholds based on allocation patterns
License
This project is licensed under the MIT License.