Collections and occupied memory

My question is can the collection (in my case hashmap) occupy the same amount of memory in the canister after it is cleaned? Let’s say I cleared it by assigning hm := HashMap.HashMap<K,V>(0, equal, hash); but it looks like the data remains in the canister. How to clean up completely, by calling delete in a loop, or are there options?

This is expected: the canister as such can only grow it’s memory size, but not shrink it. So when you use a big value and then drop it, the canister memory size does not shrink again. Instead, the freed memory will be used for the next values. In other words, the canister size won’t grow for a while.

This holds for all canisters (Motoko and Rust).

5 Likes

OK thanks. As for the cycles, how is the cost deducted if the space in the canister has been increased and is not actually used?

The system can’t tell whether the space is used, that is internal to the canister.

In essence you are paying for the high water mark of memory consumption since the last canister upgrade.

2 Likes

Can vaule of (rts_heap_size + 196608) byte describe the rts_memory in time?