pub struct Heap { /* private fields */ }Expand description
Owns the live-object registry for one JavaScript runtime.
Implementations§
Source§impl Heap
impl Heap
Sourcepub fn new() -> Heap
pub fn new() -> Heap
Create an empty heap. Runs the collector only when asked
(see Heap::set_threshold).
Examples found in repository?
examples/cycle.rs (line 20)
19fn main() {
20 let heap = Heap::new();
21 let a = Gc::new(&heap, Node::default());
22 let b = Gc::new(&heap, Node::default());
23 *a.next.borrow_mut() = Some(b.clone());
24 *b.next.borrow_mut() = Some(a.clone()); // a cycle
25 drop((a, b));
26
27 let stats = heap.collect();
28 println!("collected={} live={}", stats.collected, stats.after);
29 assert_eq!(stats.collected, 2, "the cycle should be collected");
30}Sourcepub fn set_threshold(&self, n: usize)
pub fn set_threshold(&self, n: usize)
Trigger a collection automatically when more than n boxes are live.
usize::MAX disables automatic collection (the default).
Sourcepub fn live_count(&self) -> usize
pub fn live_count(&self) -> usize
Number of live boxes.
Sourcepub fn allocated_bytes(&self) -> usize
pub fn allocated_bytes(&self) -> usize
Approximate number of bytes held by live boxes (excluding external allocations made by the values themselves).
Sourcepub fn collect(&self) -> CollectStats
pub fn collect(&self) -> CollectStats
Run the cycle collector.
Objects that are only reachable from garbage are dropped and freed; everything reachable from a handle living outside the heap survives.
Examples found in repository?
examples/cycle.rs (line 27)
19fn main() {
20 let heap = Heap::new();
21 let a = Gc::new(&heap, Node::default());
22 let b = Gc::new(&heap, Node::default());
23 *a.next.borrow_mut() = Some(b.clone());
24 *b.next.borrow_mut() = Some(a.clone()); // a cycle
25 drop((a, b));
26
27 let stats = heap.collect();
28 println!("collected={} live={}", stats.collected, stats.after);
29 assert_eq!(stats.collected, 2, "the cycle should be collected");
30}Trait Implementations§
Auto Trait Implementations§
impl !Freeze for Heap
impl !RefUnwindSafe for Heap
impl !Send for Heap
impl !Sync for Heap
impl !UnwindSafe for Heap
impl Forget for Heap
impl Move for Heap
impl Unpin for Heap
impl UnsafeUnpin for Heapwhere
*const u8 is !null: UnsafeUnpin,
PhantomData<Option<NonNull<Header>>>: UnsafeUnpin,
PhantomData<usize>: UnsafeUnpin,
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more