Motoko - Get Canisters Sizes / Limits

Is there a way to introspect canister size / limits from Motoko at the moment?

1 Like

Hi Hazel,

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

The 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,
Claudio

9 Likes

Thanks @claudio! This is perfect! Noted the instability, this works just fine for my hacks / POC stuffs.

1 Like

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

  • What is rts_memory_size() and is it correlated with “Memory Size” mentioned in dfx canister status <canister> output?

  • What is rts_reclaimed()?

  • What is rts_max_live_size()?

  • As I see from our tests rts_total_allocation() is the sum of rts_reclaimed() and rts_heap_size() - correct?

Thanks in advance…

1 Like

I think Andreas just answered most of them in Motoko, Array, Memory - #5 by rossberg

As an aside, what does RTS stand for?

1 Like

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.

4 Likes

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.

4 Likes

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