Dfx deploy --network=ic heap out of bounds

The invocation to the wallet call forward method failed with the error: An error happened during the call: 5: Canister “canister-id” trapped: heap out of bounds

Appeared for the first time? What do i do

1 Like

Is there anyone? please help me

I can’t really answer in the abstract - can you document the dfx version, the call you were trying to make, your OS, and whether you have upgraded your cycles wallet recently?

dfx version 0.7.2
Mac Os 11.4
dfx canister --network=ic status “Canister Name”
Canister status call result for “Canister Name”
Status: Running

dfx deploy --network=ic

Deploying all canisters.
All canisters have already been created.
Building canisters…

Upgrading code for canister “Canister Name”, with canister_id “canisterId”
The invocation to the wallet call forward method failed with the error: An error happened during the call: 5: Canister “canisterId” trapped: heap out of bounds

Controller: r34ha-yiaaa-aaaah-aaa4q-cai
Memory allocation: 0
Compute allocation: 0
Freezing threshold: 2_592_000
Memory Size: Nat(BigUint { data: [56467965] })
Balance: 56_206_146_962_072 Cycles
Module hash: 0x969171e08c4e6c60b54d889e3a733efbbdedfbc30536bcafbf0b6e892c68bbb7

Any news on this? I got the same error when upgrade my canister.


There is no way to contact the corresponding person to deal with it. At present, we also leave this container in a free state.

Why Memory allocation is 0?

system auto allocation
No manual adjustment

I got the same issue.
It’s the reason : HashMap too big ?
It’s any way to find the reason?

my dfx version 0.8.4

        heap_size        = Prim.rts_heap_size();
        memory_size      = Prim.rts_memory_size();
        max_live_size    = Prim.rts_max_live_size();
        total_allocation = Prim.rts_total_allocation();
        reclaimed        = Prim.rts_reclaimed();
        rts_version      = Prim.rts_version();
        cycles           = Cycles.balance();

I ran into this error last week. First of all, what I describe below may not be the only cause of this error. With that in mind, please read on…

I ran into “heap out of bounds” when trying to upgrade a motoko canister. It turned out that the error was caused by incompatible types of my stable variable. I think the next release of dfx will have better error messages that actually prevent you from doing the upgrade when such an incompatibility is detected, instead of allowing your upgrade request to be sent and then got a runtime error back. I might have jumped the gun, this may not be ready yet…

In any case, it should be easy to understand that why one can’t automatically upgrade
stable var i : Int = ... to stable var j : Nat = ... . In other words, old value of a stable variable may not satisfy its new type. So there is an entire set of subtype rules to help check this.

It is however not an easy task to check manually when your type is more complex. For example, one is allowed to add a new field to a record as long as the new field has an option type. This is okay, but I was adding a mutable field var name = ...., then this is not okay! I ran into “heap out of bound” error due to this. Eventually I resolved this problem by doing a conversion between old and new type in the postupgrade() function.

It is actually better to run into errors during upgrade. It is much worse when the upgrade went through and yet data is lost in stable variables! It can also happen! This is why I think it is crucial to have automatic checks on type compatibility of stable variables before attempting an upgrade.

Hope it helps!


Thanks for your help .

Some thing wrong in my code , I used some reference variables,when canister upgrade it will change temp variables to stable [reference variables], then temp variables will be cleared, and the reference variables lost.

type DataStruct = {
	titleIdx : Nat;
	var count : Nat;
	var datas : List.List<Text>;
private var stableDataArray : [DataStruct] = [];
private var tmpTBTree  = RBT.RBTree<Nat, DataStruct>(Nat.compare);
system preupgrade() {
    stableDataArray := Array.map<(Nat,DataStruct), DataStruct>(Iter.toArray(tmpTBTree.entries()) , func(x :(Nat,DataStruct) ) : (DataStruct) {x.1}) ;
system postupgrade() {
    for(v in stableDataArray.vals()){tmpTBTree.put(v.titleIdx, v)};

So I changed DataStruct and fix it.

type DataStructImmu = {
        titleIdx : Nat;
        count : Nat;
        datas : [Text];
1 Like