Motoko canister memory size increased after upgrade

This is not unexpected: For the upgrade, the Motoko runtime serializes your whole state to stable memory, in a format that is almost like Candid. This is not implemented in the most efficient way yet; in particular it will

  1. increase the main memory to have space to assemble the candid data.
  2. serialized the data in Candid format into that space
  3. copy it in bulk to stable memory
  4. (now the upgrade happens)
  5. increase the main memory to have space for the encoded candid data
  6. copy it in bulk from the stable memory
  7. decode from Candid into Motoko

The scratch space used in step 5 will now be unused, and reclaimed by the GC, but WebAssembly doesn’t allow code to give back memory to the system, so the memory footprint will appear large.

But Motoko will use that memory for new data before it grows the memory again, so it is altogether not too bad.

(The code change is a red herring, I assume you’d see the same behavior even if you upgrade to the same code.)

8 Likes