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 using a mark-and-sweep garbage collection algorithm.
Core Features
- Mark-and-Sweep Garbage Collection: Uses a two-phase mark-and-sweep algorithm to detect and release objects that are no longer referenced, including circularly referenced objects
- Dual Threshold Collection: Supports both percentage-based and memory-based thresholds for triggering garbage collection
- Percentage Threshold: Automatically triggers collection when attach operations exceed a configurable percentage of current object count
- Memory Threshold: Triggers collection when allocated memory exceeds a specified byte limit
- Type Safety: Completely type-safe, leveraging Rust's type system for memory safety
- Concurrency Safety: All operations are thread-safe with atomic counters and proper synchronization
- Weak Reference Support: Provides
GCArcWeaktype for solving circular reference problems - Reference Tracking: Implement the
GCTraceabletrait to make objects part of the garbage collection system - Memory Tracking: Tracks allocated memory usage for better resource management
Usage
Installation
Use cargo add rust-arc-gc to add the library to your project.
API Reference
GC
Constructor Methods
GC::new()- Create a new garbage collector instance with default 20% percentage thresholdGC::new_with_percentage(percentage)- Create a garbage collector with custom percentage threshold (e.g., 30 for 30%)GC::new_with_memory_threshold(memory_threshold)- Create a garbage collector with memory threshold in bytesGC::new_with_thresholds(percentage, memory_threshold)- Create a garbage collector with both percentage and memory thresholds
Object Management Methods
gc.attach(obj)- Add an object to the garbage collector's tracking scope (may trigger automatic collection)gc.detach(obj)- Remove an object from garbage collector tracking, returnstrueif object was found and removedgc.create(obj)- Create a new object and automatically add it to the garbage collectorgc.collect()- Manually perform mark-and-sweep garbage collection
Information Methods
gc.object_count()- Return the current number of objects managed by the garbage collectorgc.get_all()- Return a vector of all objects currently managed by the garbage collectorgc.allocated_memory()- Get the current estimated allocated memory in bytesgc.memory_threshold()- Get the current memory threshold settinggc.set_memory_threshold(threshold)- Set or update the memory threshold (None to disable)
Collection Triggering
The garbage collector uses multiple strategies to decide when to trigger collection:
- Percentage Threshold: Triggers when
attach_count >= current_objects * (percentage / 100) - Memory Threshold: Triggers when
allocated_memory >= memory_threshold(if set) - Manual Triggering: Always available via
collect()method
Both thresholds (if configured) work independently - collection triggers when either condition is met.
GCArc
GCArc::new(obj)- Create a new reference-counted objectarc.as_ref()- Get an immutable reference to the objectarc.get_mut()- Get a mutable reference to the object (panics if not unique)arc.try_as_mut()- Try to get a mutable reference, returnsOption<&mut T>arc.as_weak()- Create a weak reference to the objectarc.strong_ref()- Get the current strong reference countarc.weak_ref()- Get the current weak reference count
GCTraceable
A trait that must be implemented to allow the garbage collector to track object references:
Implementation Guidelines:
- Add any
GCArcWeak<T>references held by your object to the queue - This enables the garbage collector to traverse your object's references during the mark phase
- For objects with no references to other GC objects, an empty implementation is sufficient
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)weak.strong_ref()- Get the current strong reference countweak.weak_ref()- Get the current weak reference count
Usage Example
use ;
use VecDeque;
use RefCell;
// Define a node structure with potential circular references
Performance Considerations
The dual-threshold collection system provides flexible memory management:
Threshold Configuration
- Percentage Threshold (default: 20%):
- Lower percentages (10-15%): More frequent collection, lower memory usage, higher CPU overhead
- Higher percentages (30-50%): Less frequent collection, potentially higher memory usage, lower CPU overhead
- Memory Threshold: Set absolute memory limits for memory-constrained environments
- Combined Thresholds: Use both for fine-tuned control
Collection Algorithm
- Mark-and-Sweep: Two-phase algorithm ensuring complete cycle detection
- Root Detection: Identifies objects with external references as collection roots
- Thread Safety: Atomic operations minimize locking overhead
- Memory Tracking: Estimates memory usage for threshold-based collection
Optimization Tips
- Use
try_as_mut()instead ofget_mut()when mutation might fail - Prefer
as_weak()for non-owning references to prevent cycles - Consider memory thresholds for applications with predictable memory patterns
- Monitor
allocated_memory()andobject_count()for tuning thresholds
Limitations and Future Plans
Current Limitations
- Collection Algorithm: Uses mark-and-sweep which may cause brief pauses during collection
- Memory Estimation: Object size estimation is approximate and may not account for all heap allocations
- Single-threaded Collection: Collection process is not parallelized
Future Enhancements
- Incremental Collection: Reduce pause times by spreading collection work across multiple cycles
- Generational Collection: Optimize for typical allocation patterns with generational hypothesis
- Parallel Collection: Utilize multiple threads during mark and sweep phases
- Adaptive Thresholds: Automatically adjust thresholds based on allocation patterns and performance metrics
- Enhanced Debugging: Add memory usage statistics, collection timing, and object lifecycle tracking
- Custom Allocators: Integration with custom memory allocators for better memory tracking
License
This project is licensed under the MIT License.