# How does the upgrading of canister works

**URL:** <https://forum.dfinity.org/t/how-does-the-upgrading-of-canister-works/20651>\
**Category:** Developers\
**Tags:** Discussing\
**Created:** [June 12, 2023, 6:37am UTC](https://forum.dfinity.org/t/how-does-the-upgrading-of-canister-works/20651 "2023-06-12T06:37:12Z")\
**Posts on this page:** 19\
**Page:** 1

<div class="post-metadata">

**Author:** ![pramitgaha](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/pramitgaha/32/11659_2.png) [@pramitgaha](https://forum.dfinity.org/u/pramitgaha)\
**Post date:** [June 12, 2023, 6:37am UTC](https://forum.dfinity.org/t/how-does-the-upgrading-of-canister-works/20651/1 "2023-06-12T06:37:12Z")

</div>

I’m using `ic-stable-memory` libary by @senior.joinu.  
here is my upgrading code:

```rs
#[pre_upgrade]
pub fn pre_upgrade() {
    let init_state: InitState = INIT_STATE.with(|state| state.borrow().clone());
    let init_state = SBox::new(init_state).expect("failed to clone");
    let system_state = SYSTEM_STATE
        .with(|state| state.borrow().clone())
        .expect("failed to clone");
    let system_state = SBox::new(system_state).expect("failed to box");
    let user_state = USER_STATE.with(|state| state.borrow().clone().expect("failed to clone"));
    let user_state = SBox::new(user_state).expect("failed to box");
    let offer_state = OFFER_STATE.with(|state| state.borrow().clone().expect("failed to clonex"));
    let offer_state = SBox::new(offer_state).expect("failed to box");
    store_custom_data(0, init_state);
    store_custom_data(1, system_state);
    store_custom_data(2, user_state);
    store_custom_data(3, offer_state);
    stable_memory_pre_upgrade().expect("out of memory");
}

#[post_upgrade]
pub fn post_upgrade() {
    stable_memory_post_upgrade();
    let init_state: SBox<InitState> = retrieve_custom_data(0).unwrap();
    let system_state: SBox<System> = retrieve_custom_data(1).unwrap();
    let user_state: SBox<UserState> = retrieve_custom_data(2).unwrap();
    let offer_state: SBox<OfferState> = retrieve_custom_data(3).unwrap();
    INIT_STATE.with(|state| {
        *state.borrow_mut() = init_state.into_inner();
    });
    SYSTEM_STATE.with(|state| {
        *state.borrow_mut() = system_state.into_inner();
    });
    USER_STATE.with(|state| {
        *state.borrow_mut() = user_state.into_inner();
    });
    OFFER_STATE.with(|state| {
        *state.borrow_mut() = offer_state.into_inner();
    });
}

```

when I try to upgrade the canister it fails with this error:

```auto
Error: Failed while trying to deploy canisters.
Caused by: Failed while trying to deploy canisters.
  Failed while trying to install all canisters.
    Failed to install wasm module to canister 'contract'.
      Failed during wasm installation call: The Replica returned an error: code 5, message: "Canister bw4dl-smaaa-aaaaa-qaacq-cai trapped: heap out of bounds"

```

---

<div class="post-metadata">

**Author:** ![senior.joinu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/senior.joinu/32/9240_2.png) [@senior.joinu](https://forum.dfinity.org/u/senior.joinu)\
**Post date:** [June 12, 2023, 9:43am UTC](https://forum.dfinity.org/t/how-does-the-upgrading-of-canister-works/20651/2 "2023-06-12T09:43:50Z")

</div>

Can you please also provide us with your `init` method and how you declare the state (thread\_locals)?

Does this error happen on upgrade or on initial deployment?

UPD:  
also, how much heap memory does your state occupy before upgrading?

---

<div class="post-metadata">

**Author:** ![pramitgaha](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/pramitgaha/32/11659_2.png) [@pramitgaha](https://forum.dfinity.org/u/pramitgaha)\
**Post date:** [June 13, 2023, 4:37am UTC](https://forum.dfinity.org/t/how-does-the-upgrading-of-canister-works/20651/3 "2023-06-13T04:37:12Z")

</div>

```rs
thread_local! {
    pub static INIT_STATE: RefCell<InitState> = RefCell::default();
    pub static OFFER_STATE: RefCell<OfferState> = RefCell::default();
    pub static USER_STATE: RefCell<UserState> = RefCell::default();
    pub static SYSTEM_STATE: RefCell<System> = RefCell::default();
}

```

How do I check how much storage is being used, I don’t know!

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [June 13, 2023, 7:14am UTC](https://forum.dfinity.org/t/how-does-the-upgrading-of-canister-works/20651/4 "2023-06-13T07:14:13Z")

</div>

There is a [memory field in the canister status response](https://docs.rs/ic-cdk/latest/ic_cdk/api/management_canister/main/struct.CanisterStatusResponse.html#structfield.memory_size). Is that useful for you?

---

<div class="post-metadata">

**Author:** ![pramitgaha](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/pramitgaha/32/11659_2.png) [@pramitgaha](https://forum.dfinity.org/u/pramitgaha)\
**Post date:** [June 13, 2023, 8:21am UTC](https://forum.dfinity.org/t/how-does-the-upgrading-of-canister-works/20651/5 "2023-06-13T08:21:27Z")

</div>

is there heap limit for upgrading the codes?

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [June 13, 2023, 8:40am UTC](https://forum.dfinity.org/t/how-does-the-upgrading-of-canister-works/20651/6 "2023-06-13T08:40:32Z")

</div>

Yes, the Wasm runtime is only 32 bit for now, so you can only have up to 4GB in the heap

---

<div class="post-metadata">

**Author:** ![pramitgaha](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/pramitgaha/32/11659_2.png) [@pramitgaha](https://forum.dfinity.org/u/pramitgaha)\
**Post date:** [June 13, 2023, 8:42am UTC](https://forum.dfinity.org/t/how-does-the-upgrading-of-canister-works/20651/7 "2023-06-13T08:42:06Z")

</div>

damn, what if my canister holds more data than that of 4gb?  
what’s the say to make it work at the moment?

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [June 13, 2023, 8:43am UTC](https://forum.dfinity.org/t/how-does-the-upgrading-of-canister-works/20651/8 "2023-06-13T08:43:06Z")

</div>

Then you have to store data in stable storage. Stable storage goes up to 48 GB (or is it 64 already?)

---

<div class="post-metadata">

**Author:** ![pramitgaha](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/pramitgaha/32/11659_2.png) [@pramitgaha](https://forum.dfinity.org/u/pramitgaha)\
**Post date:** [June 13, 2023, 8:46am UTC](https://forum.dfinity.org/t/how-does-the-upgrading-of-canister-works/20651/9 "2023-06-13T08:46:32Z")

</div>

hmm, how do I do that?  
can you show some code?  
I’m using `ic-stable-memory` so I’m guessing my data is already on stable-memory.  
is that how it’s done? or I’m wrong?

---

<div class="post-metadata">

**Author:** ![pramitgaha](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/pramitgaha/32/11659_2.png) [@pramitgaha](https://forum.dfinity.org/u/pramitgaha)\
**Post date:** [June 13, 2023, 8:54am UTC](https://forum.dfinity.org/t/how-does-the-upgrading-of-canister-works/20651/10 "2023-06-13T08:54:27Z")

</div>

also in the above code/project. I just have inserted few bytes of data. why did it failed?

---

<div class="post-metadata">

**Author:** ![Vivienne](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/vivienne/32/29170_2.png) [@Vivienne](https://forum.dfinity.org/u/Vivienne)\
**Post date:** [June 13, 2023, 9:11am UTC](https://forum.dfinity.org/t/how-does-the-upgrading-of-canister-works/20651/11 "2023-06-13T09:11:09Z")

</div>

I’m no good with ic-stable-memory, but I can recommend you have a look at the [readme](https://github.com/seniorjoinu/ic-stable-memory) to see some examples. Otherwise, please ping @senior.joinu, he’s the author

---

<div class="post-metadata">

**Author:** ![senior.joinu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/senior.joinu/32/9240_2.png) [@senior.joinu](https://forum.dfinity.org/u/senior.joinu)\
**Post date:** [June 13, 2023, 9:16am UTC](https://forum.dfinity.org/t/how-does-the-upgrading-of-canister-works/20651/12 "2023-06-13T09:16:15Z")

</div>

I would suggest you to provide us with a MRE. This way I could try it locally and see what happens.  
From the error message itself I don’t see if this is a bug with `ic-stable-memory`, but it may be.

---

<div class="post-metadata">

**Author:** ![pramitgaha](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/pramitgaha/32/11659_2.png) [@pramitgaha](https://forum.dfinity.org/u/pramitgaha)\
**Post date:** [June 13, 2023, 9:20am UTC](https://forum.dfinity.org/t/how-does-the-upgrading-of-canister-works/20651/13 "2023-06-13T09:20:40Z")

</div>

what’s MRE?  
here is my init method:

```rs
pub fn init() {
    ic_stable_memory::stable_memory_init();
    let caller = ic_cdk::caller();
    INIT_STATE.with(|state| {
        let mut state = state.borrow_mut();
        state.authority = caller;
    })
}

```

---

<div class="post-metadata">

**Author:** ![senior.joinu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/senior.joinu/32/9240_2.png) [@senior.joinu](https://forum.dfinity.org/u/senior.joinu)\
**Post date:** [June 13, 2023, 9:24am UTC](https://forum.dfinity.org/t/how-does-the-upgrading-of-canister-works/20651/14 "2023-06-13T09:24:37Z")

</div>

A minimal reproducible example.

---

<div class="post-metadata">

**Author:** ![pramitgaha](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/pramitgaha/32/11659_2.png) [@pramitgaha](https://forum.dfinity.org/u/pramitgaha)\
**Post date:** [June 13, 2023, 9:35am UTC](https://forum.dfinity.org/t/how-does-the-upgrading-of-canister-works/20651/15 "2023-06-13T09:35:28Z")

</div>

can you explain what do i have to share?

---

<div class="post-metadata">

**Author:** ![senior.joinu](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/senior.joinu/32/9240_2.png) [@senior.joinu](https://forum.dfinity.org/u/senior.joinu)\
**Post date:** [June 13, 2023, 9:40am UTC](https://forum.dfinity.org/t/how-does-the-upgrading-of-canister-works/20651/16 "2023-06-13T09:40:06Z")

</div>

Yes, sure.  
So you have this code that doesn’t work. If it is not a secret, just share it without modifying and provide an instruction on how to achieve the same error you’re having.  
If it is a secret, then find a way to achieve the same error with similar code, that does not reveal any secrets, and share this code instead.

---

<div class="post-metadata">

**Author:** ![pramitgaha](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/pramitgaha/32/11659_2.png) [@pramitgaha](https://forum.dfinity.org/u/pramitgaha)\
**Post date:** [July 13, 2023, 2:08am UTC](https://forum.dfinity.org/t/how-does-the-upgrading-of-canister-works/20651/17 "2023-07-13T02:08:28Z")

</div>

hello @Vivienne, does the memory size returned from the method is heap storage or it is also the combination of stable memory too?

---

<div class="post-metadata">

**Author:** ![ulan](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/ulan/32/2705_2.png) [@ulan](https://forum.dfinity.org/u/ulan)\
**Post date:** [July 13, 2023, 1:41pm UTC](https://forum.dfinity.org/t/how-does-the-upgrading-of-canister-works/20651/18 "2023-07-13T13:41:59Z")

</div>

It the sum of Wasm and stable memory sizes.

---

<div class="post-metadata">

**Author:** ![lastmjs](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/lastmjs/32/3406_2.png) [@lastmjs](https://forum.dfinity.org/u/lastmjs)\
**Post date:** [July 13, 2023, 4:13pm UTC](https://forum.dfinity.org/t/how-does-the-upgrading-of-canister-works/20651/19 "2023-07-13T16:13:10Z")

</div>

It’s been 64 GiB for a bit now.
