# Easiest way to know when canister size gets to certain size

**URL:** <https://forum.dfinity.org/t/easiest-way-to-know-when-canister-size-gets-to-certain-size/13838>\
**Category:** Getting Started\
**Created:** [June 16, 2022, 11:41am UTC](https://forum.dfinity.org/t/easiest-way-to-know-when-canister-size-gets-to-certain-size/13838 "2022-06-16T11:41:27Z")\
**Posts on this page:** 11\
**Page:** 1

<div class="post-metadata">

**Author:** ![AnonymousCoder](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/anonymouscoder/32/4747_2.png) [@AnonymousCoder](https://forum.dfinity.org/u/AnonymousCoder)\
**Post date:** [June 16, 2022, 11:41am UTC](https://forum.dfinity.org/t/easiest-way-to-know-when-canister-size-gets-to-certain-size/13838/1 "2022-06-16T11:41:27Z")

</div>

Hi all!

I want to know what is the easiest way of checking current canister size in the code itself of that canister so that I can know when is the time to either:

1. Stop receiving further calls that add more data to it (to prevent issues with canister size getting too large, which would cause issues in upgrades etc)
2. Branch off from that canister to a fresh new canister with the same codebase but empty data

Is there an easy way of just setting a “MAX\_CANISTER\_SIZE” Nat and then checking against that in any method that would try and add more data to it?

---

<div class="post-metadata">

**Author:** ![domwoe](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/domwoe/32/5165_2.png) [@domwoe](https://forum.dfinity.org/u/domwoe)\
**Post date:** [June 16, 2022, 12:05pm UTC](https://forum.dfinity.org/t/easiest-way-to-know-when-canister-size-gets-to-certain-size/13838/2 "2022-06-16T12:05:29Z")

</div>

In Motoko you can use `Prim.rts_memory_size()` and in Rust it seems to be [memory\_size in core::arch::wasm32 - Rust](https://doc.rust-lang.org/core/arch/wasm32/fn.memory_size.html) .

Have a look at this thread for more details: [Motoko, Array, Memory](https://forum.dfinity.org/t/motoko-array-memory/5324)

This repo does this in Rust:

> **[GitHub - GLicDEV/quickstart\_scaling: This is a study on IC scalability of...](https://github.com/GLicDEV/quickstart_scaling)**
>
> This is a study on IC scalability of smart contracts using rust - GitHub - GLicDEV/quickstart\_scaling: This is a study on IC scalability of smart contracts using rust

and there might be other examples in the following thread: [ICDevs.org Bounty #20 - QuickStart Dapp - Scaling With Canisters - 200 ICP, 100 ICP, 50 ICP - Multiple winners](https://forum.dfinity.org/t/icdevs-org-bounty-20-quickstart-dapp-scaling-with-canisters-200-icp-100-icp-50-icp-multiple-winners/11756)

---

<div class="post-metadata">

**Author:** ![AnonymousCoder](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/anonymouscoder/32/4747_2.png) [@AnonymousCoder](https://forum.dfinity.org/u/AnonymousCoder)\
**Post date:** [June 16, 2022, 12:19pm UTC](https://forum.dfinity.org/t/easiest-way-to-know-when-canister-size-gets-to-certain-size/13838/3 "2022-06-16T12:19:30Z")

</div>

Thank you very much 🙂 I’ve seen this in another thread around the forum as well, but it was said that it’s a “risk” using it since it may change in the future. How viable would this be to use for a platform that would probably get in trouble if this stops working at a certain point and there were already hundreds of deployed smart contracts from the “factory” contract deploying them using now obsolete code for checking the storage? Would it be a terrible (or impossible task) of upgrading code later on in these “auto-deployed” contracts if this happens?

---

<div class="post-metadata">

**Author:** ![alexeychirkov](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/alexeychirkov/32/1991_2.png) [@alexeychirkov](https://forum.dfinity.org/u/alexeychirkov)\
**Post date:** [June 16, 2022, 12:20pm UTC](https://forum.dfinity.org/t/easiest-way-to-know-when-canister-size-gets-to-certain-size/13838/4 "2022-06-16T12:20:34Z")

</div>

Please take a look at how we are getting memory size at Canistergeek (motoko and rust):

> <https://github.com/usergeek/canistergeek-ic-motoko/blob/master/src/typesModule.mo#L276>

> <https://github.com/usergeek/canistergeek_ic_rust/blob/main/src/ic_util/mod.rs#L31>

---

<div class="post-metadata">

**Author:** ![domwoe](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/domwoe/32/5165_2.png) [@domwoe](https://forum.dfinity.org/u/domwoe)\
**Post date:** [June 16, 2022, 12:33pm UTC](https://forum.dfinity.org/t/easiest-way-to-know-when-canister-size-gets-to-certain-size/13838/5 "2022-06-16T12:33:48Z")

</div>

Thanks Alexey! I wanted to mention Canistergeek but forgot.

> [@AnonymousCoder](#):
>
> Thank you very much 🙂 I’ve seen this in another thread around the forum as well, but it was said that it’s a “risk” using it since it may change in the future. How viable would this be to use for a platform that would probably get in trouble if this stops working at a certain point and there were already hundreds of deployed smart contracts from the “factory” contract deploying them using now obsolete code for checking the storage? Would it be a terrible (or impossible task) of upgrading code later on in these “auto-deployed” contracts if this happens?

Good question. I’ll forward this internally.

---

<div class="post-metadata">

**Author:** ![AnonymousCoder](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/anonymouscoder/32/4747_2.png) [@AnonymousCoder](https://forum.dfinity.org/u/AnonymousCoder)\
**Post date:** [June 16, 2022, 1:45pm UTC](https://forum.dfinity.org/t/easiest-way-to-know-when-canister-size-gets-to-certain-size/13838/6 "2022-06-16T13:45:12Z")

</div>

Thank you for this, I didn’t know such service exists 🙂 To me, it seems like this tool provides measurements for how much data a specific canister implementing this library is using at specific time during the day? So, since I just need to know this info at any method that might store more data into the canister, I would just need to use `Prim.rts_memory_size()` that your library also uses in the above `collectMemory()` method and check against the MAX\_CANISTER\_SIZE variable that I can set initially in the canister as the largest size of the canister I want to allow? Something like:

```auto
let MAX_CANISTER_SIZE: Nat = 2_000_000_000; // this would be approx 2GB of storage?
  public query func someMethodThatWouldWantToStoreMoreData(data: Blob): async Text {
    if(Prim.rts_memory_size() < MAX_CANISTER_SIZE) {
      // we can do further checks for the passed in data to see if it's size + Prim.rts_memory_size is still smaller than MAX_CANISTER_SIZE and error out if not

      // if all is good, we can add new data to the contract here
    } else {
      // we return the error
    }
  }

```

Or am I completely missing something here? 🙂

---

<div class="post-metadata">

**Author:** ![domwoe](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/domwoe/32/5165_2.png) [@domwoe](https://forum.dfinity.org/u/domwoe)\
**Post date:** [June 16, 2022, 2:45pm UTC](https://forum.dfinity.org/t/easiest-way-to-know-when-canister-size-gets-to-certain-size/13838/7 "2022-06-16T14:45:33Z")

</div>

> [@AnonymousCoder](#):
>
> Thank you very much 🙂 I’ve seen this in another thread around the forum as well, but it was said that it’s a “risk” using it since it may change in the future. How viable would this be to use for a platform that would probably get in trouble if this stops working at a certain point and there were already hundreds of deployed smart contracts from the “factory” contract deploying them using now obsolete code for checking the storage? Would it be a terrible (or impossible task) of upgrading code later on in these “auto-deployed” contracts if this happens?

I chatted with @dsarlis about this. There is a risk, but the risk is essentially that there are non-backwards compatible changes to WASM, since we are not building our own WASM runtime but use the one developed by the Byte Code Alliance. The risk for this seems to be rather low.

> [@AnonymousCoder](#):
>
> I would just need to use `Prim.rts_memory_size()` that your library also uses in the above `collectMemory()` method and check against the MAX\_CANISTER\_SIZE variable that I can set initially in the canister as the largest size of the canister I want to allow?

Yep, with two comments:

1. Your example is a query, so would not allow to store data anyway 😉 . If you’re mostly concerned with data from outside the IC you could (additionally) use the `inspect_message` hook to check for this without having to spend cycles.

2. You might want to make `MAX_CANISTER_SIZE` changeable via some form of administrative action, since upgrades to the system could allow to safely increase the limit.

---

<div class="post-metadata">

**Author:** ![AnonymousCoder](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/anonymouscoder/32/4747_2.png) [@AnonymousCoder](https://forum.dfinity.org/u/AnonymousCoder)\
**Post date:** [June 16, 2022, 2:50pm UTC](https://forum.dfinity.org/t/easiest-way-to-know-when-canister-size-gets-to-certain-size/13838/8 "2022-06-16T14:50:09Z")

</div>

> [@domwoe](#):
>
> If you’re mostly concerned with data from outside the IC you could (additionally) use the `inspect_message` hook to check for this without having to spend cycles.

Thank you very much for the confirmation 🙂 Sorry, I just used some sample method that I doodled real quick to make it into a pseudo code, did not even see it’s a query method 😃 Could you just explain a bit more on the quote ? What’s the `inspect_message`, never seen it before, is it a part of one of the core ICP libraries or something else?

---

<div class="post-metadata">

**Author:** ![domwoe](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/domwoe/32/5165_2.png) [@domwoe](https://forum.dfinity.org/u/domwoe)\
**Post date:** [June 16, 2022, 3:08pm UTC](https://forum.dfinity.org/t/easiest-way-to-know-when-canister-size-gets-to-certain-size/13838/9 "2022-06-16T15:08:58Z")

</div>

Sure, your canister can use [this hook](https://internetcomputer.org/docs/current/references/ic-interface-spec#system-api-inspect-message) to decide if it wants accept an ingress message or deny it without going through consensus.

Here’s how you do it in Rust: [inspect\_message in ic\_cdk\_macros - Rust](https://docs.rs/ic-cdk-macros/latest/ic_cdk_macros/attr.inspect_message.html)

In Motoko the feature was only recently exposed: [Release 0.6.28 · dfinity/motoko · GitHub](https://github.com/dfinity/motoko/releases/tag/0.6.28)

---

<div class="post-metadata">

**Author:** ![AnonymousCoder](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/anonymouscoder/32/4747_2.png) [@AnonymousCoder](https://forum.dfinity.org/u/AnonymousCoder)\
**Post date:** [June 16, 2022, 3:13pm UTC](https://forum.dfinity.org/t/easiest-way-to-know-when-canister-size-gets-to-certain-size/13838/10 "2022-06-16T15:13:02Z")

</div>

Very nice, ty good ser! All of this will be very useful 🙂 Thank you both

---

<div class="post-metadata">

**Author:** ![panch8](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/panch8/32/9250_2.png) [@panch8](https://forum.dfinity.org/u/panch8)\
**Post date:** [July 9, 2024, 9:37am UTC](https://forum.dfinity.org/t/easiest-way-to-know-when-canister-size-gets-to-certain-size/13838/11 "2024-07-09T09:37:17Z")

</div>

> [@domwoe](#):
>
> `inspect_message` hoo

Nice @domwoe thx!!! always learning here!
