|
1 | 1 | # Low Wasm memory hook |
2 | 2 |
|
3 | | -The Internet Computer can automatically execute a special type of function called a low Wasm memory hook, which runs when the canister's available Wasm memory falls below the `wasm_memory_threshold`. |
| 3 | +This example demonstrates the low Wasm memory hook on the Internet Computer. The ICP runtime automatically executes a special `lowmemory` system function when a canister's available Wasm memory falls below the configured `wasm_memory_threshold`. The example shows the execution order of the `heartbeat` and `lowmemory` system functions as the canister progressively allocates memory until the threshold is reached. |
4 | 4 |
|
5 | | -This Motoko example demonstrates using the low Wasm memory hook on ICP. For more information take a look at [low Wasm memory hook specification](https://internetcomputer.org/docs/references/ic-interface-spec#on-low-wasm-memory). |
| 5 | +For more information, see the [low Wasm memory hook specification](https://docs.internetcomputer.org/references/ic-interface-spec/canister-interface/#on-low-wasm-memory). |
6 | 6 |
|
7 | | -## Deploying from ICP Ninja |
| 7 | +## Build and deploy from the command line |
8 | 8 |
|
9 | | -[](https://icp.ninja/editor?g=https://github.com/dfinity/examples/tree/master/motoko/low_wasm_memory) |
| 9 | +### Prerequisites |
10 | 10 |
|
11 | | -## Build and deploy from the command-line |
| 11 | +- Node.js |
| 12 | +- icp-cli: `npm install -g @icp-sdk/icp-cli @icp-sdk/ic-wasm` |
| 13 | +- ic-mops: `npm install -g ic-mops` |
12 | 14 |
|
13 | | -### 1. [Download and install the IC SDK.](https://internetcomputer.org/docs/building-apps/getting-started/install) |
| 15 | +### Install |
14 | 16 |
|
15 | | -### 2. Download your project from ICP Ninja using the 'Download files' button on the upper left corner, or [clone the GitHub examples repository.](https://github.com/dfinity/examples/) |
16 | | - |
17 | | -### 3. Navigate into the project's directory. |
18 | | - |
19 | | -### 4. Deploy the project to your local environment: |
20 | | - |
21 | | -``` |
22 | | -dfx start --background --clean && dfx deploy |
| 17 | +```bash |
| 18 | +git clone https://github.com/dfinity/examples |
| 19 | +cd examples/motoko/low_wasm_memory |
23 | 20 | ``` |
24 | 21 |
|
25 | | -After the deployment, the memory usage periodically increases as defined in the `heartbeat` function. |
| 22 | +### Deploy and test |
26 | 23 |
|
27 | | -The `dfx canister update-settings` command sets the 'wasm_memory_limit' to 5MiB and 'wasm_memory_threshold' to 2MiB. |
28 | | -Hence, whenever the Wasm memory used by the canister is above 3MiB (in other words, the remaining Wasm memory is less than 'wasm_memory_threshold'), the low Wasm memory hook will be triggered. |
29 | | - |
30 | | -This Motoko canister starts with ~2.3MiB of memory usage after the deployment, so it will trigger the low Wasm memory hook after allocating ~0.7MiB of memory. |
| 24 | +```bash |
| 25 | +icp network start -d |
| 26 | +icp deploy |
| 27 | +make test |
| 28 | +icp network stop |
| 29 | +``` |
31 | 30 |
|
32 | | -You can verify that the canister settings got updated and check the current 'Memory Size' with the following command: |
| 31 | +`make test` does two things: |
33 | 32 |
|
34 | | -```sh |
35 | | -dfx canister status low_wasm_memory_hook |
36 | | -``` |
| 33 | +1. **Configures the canister settings**: sets `wasm_memory_limit` to 5 MiB and `wasm_memory_threshold` to 2 MiB. This means the `lowmemory` hook fires when remaining Wasm memory falls below 2 MiB (i.e. when usage exceeds 3 MiB). The canister starts with ~2.3 MiB of usage after deployment, so the hook triggers after allocating roughly 0.7 MiB more. |
37 | 34 |
|
38 | | -After a few seconds, observe the output of the `getExecutedFunctionsOrder` query: |
| 35 | +2. **Polls `getExecutedFunctionsOrder`** until `onLowWasmMemory` appears as the last entry (or times out after 60 s). |
39 | 36 |
|
40 | | -Query the canister by calling `getExecutedFunctionsOrder` to get the order of executed functions. |
| 37 | +To observe the execution order manually: |
41 | 38 |
|
42 | | -```sh |
43 | | -dfx canister call low_wasm_memory_hook --query getExecutedFunctionsOrder |
| 39 | +```bash |
| 40 | +icp canister call --query backend getExecutedFunctionsOrder '()' |
44 | 41 | ``` |
45 | 42 |
|
46 | | -Repeat the call until the last executed method is `onLowWasmMemory`. |
| 43 | +Repeat until the last entry is `onLowWasmMemory`. |
47 | 44 |
|
48 | 45 | ## Security considerations and best practices |
49 | 46 |
|
50 | | -If you base your application on this example, it is recommended that you familiarize yourself with and adhere to the [security best practices](https://internetcomputer.org/docs/building-apps/security/overview) for developing on ICP. This example may not implement all the best practices. |
| 47 | +Refer to the [security best practices](https://docs.internetcomputer.org/guides/security/overview) for information on security and best practices for your ICP app. |
0 commit comments