Skip to main content

Heap

Struct Heap 

Source
pub struct Heap { /* private fields */ }
Expand description

Owns the live-object registry for one JavaScript runtime.

Implementations§

Source§

impl Heap

Source

pub fn new() -> Heap

Create an empty heap. Runs the collector only when asked (see Heap::set_threshold).

Source

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.

Source

pub fn memory_limit(&self) -> Option<usize>

The current limit, if any.

Source

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.

Source

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).

Source

pub fn used_bytes(&self) -> usize

Bytes the heap accounts for: live boxes plus the payloads they reported.

Source

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.

Source

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.

Source

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).

Source

pub fn threshold(&self) -> usize

Current automatic-collection threshold.

Source

pub fn live_count(&self) -> usize

Number of live boxes.

Source

pub fn allocated_bytes(&self) -> usize

Approximate number of bytes held by live boxes (excluding external allocations made by the values themselves).

Source

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.

Trait Implementations§

Source§

impl Default for Heap

Source§

fn default() -> Heap

Returns the “default value” for a type. Read more

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
where *const u8 is !null: Unpin, *const Header is !null: Unpin,

§

impl UnsafeUnpin for Heap

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.