Is there a way to introspect canister size / limits from Motoko at the moment?
There no official library at the moment, but if you are desperate, and with the caveat that these APIs may change, there’s some basic stats provided but the
Prim (pseudo) library:
rts_version : () -> Text rts_memory_size: () -> Nat; rts_heap_size: () -> Nat; rts_total_allocation: () -> Nat; rts_reclaimed : () -> Nat; rts_max_live_size : () -> Nat;
(Sizes are in bytes.)
You can access
Prim with a special import (as do many base libraries):
import Prim "mo:prim"; ...Prim.rts_memory_size()...
Prim module is not intended to be accessed by users but is used to implement
motoko-base. It’s API is liable to change so use at your own risk/discretion.
Note that Motoko currently uses a 2-space copying collector so the heap will be at most half the memory, and the heap is thus bounded by 2GB. We have another, compacting, GC in the pipeline that will allow access to much more of the 4GB for the heap.
Hope that helps,
Thanks @claudio! This is perfect! Noted the instability, this works just fine for my hacks / POC stuffs.
@claudio can you please describe what those getters really mean?
I could not find any comments in the source code about what kind of bytes those functions corresponds to.
rts_memory_size()and is it correlated with “Memory Size” mentioned in
dfx canister status <canister>output?
As I see from our tests
rts_total_allocation()is the sum of
Thanks in advance…
I think Andreas just answered most of them in Motoko, Array, Memory - #5 by rossberg
As an aside, what does RTS stand for?
Run time system. Think of it as all the code that’s included in the resulting wasm module, but that you didn’t write nor import via modules, that comes with the compiler and that is (roughly) the same for all Motoko canisters. It contains, among other things, the garbage collector.
RTS is short for Run Time System and typically refers to infrastructure supporting program execution such as garbage collection, serialisation and low-level system calls.
Are these those refs: motoko/prim.ml at master · dfinity/motoko · GitHub
Did you ever figure out what
rts_total_allocation is? I see it keeps growing in my logs
Answer is here