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?
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_memory_limit(&self, limit: Option<usize>)
pub fn set_memory_limit(&self, limit: Option<usize>)
Cap the heap at limit bytes of GC-managed objects, or remove the cap with None.
This is what qjs --memory-limit sets. It is checked by Heap::fits, which the
engine calls before an allocation whose size it can predict (new Array(n),
s.repeat(n)) and at its own checkpoints.
Sourcepub fn memory_limit(&self) -> Option<usize>
pub fn memory_limit(&self) -> Option<usize>
The current limit, if any.
Sourcepub fn measure_used(&self) -> usize
pub fn measure_used(&self) -> usize
Measure what the heap really holds: every live box plus its payload.
O(live boxes), so it is not something to do per instruction — but it is the only
number that cannot drift, and the engine calls it on a schedule (see
Interp::check_memory) rather than tracking every mutation. Tracking would mean a
&Heap threaded through every array and map mutator in the engine, and one forgotten
call site would make the limit quietly wrong; measuring cannot forget.
Sourcepub fn fits(&self, payload: usize) -> bool
pub fn fits(&self, payload: usize) -> bool
Whether payload more bytes fit under the limit.
payload is what the caller is about to hold — the length of a string it is
building, the element vector a container is growing into — not the increment.
Passing the increment would check every push against a fresh budget and never trip.
A failed check collects once and asks again. That is not politeness: a script that
fills the limit, catches the error and drops the reference must be able to carry on,
and the reference behaves that way (js_malloc triggers a GC and retries).
Sourcepub fn used_bytes(&self) -> usize
pub fn used_bytes(&self) -> usize
Bytes the heap accounts for: live boxes plus the payloads they reported.
Sourcepub fn adjust_payload(&self, delta: isize)
pub fn adjust_payload(&self, delta: isize)
Record that a live box grew or shrank by delta bytes of payload.
Containers call this when they change in place — a.push(x) grows an element vector
without allocating a box — so that the running total stays equal to what
Trace::external_bytes will report when the box is freed. The two must agree
exactly, or the subtraction at free time would leave the counter permanently wrong.
Sourcepub fn over_limit(&self) -> bool
pub fn over_limit(&self) -> bool
Whether the heap is over its limit right now, collecting first if it might be.
The interpreter asks this at its checkpoints — a call, a backward branch — which is as often as it can cheaply ask, and often enough that a loop allocating an object per iteration stops within a few allocations of the limit.
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?
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}