# Stable memory flag

**URL:** https://forum.dfinity.org/t/stable-memory-flag/10270
**Category:** Language Support
**Tags:** Motoko
**Created:** [January 13, 2022, 2:14pm UTC](https://forum.dfinity.org/t/stable-memory-flag/10270 "2022-01-13T14:14:39Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![C-B-Elite](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/c-b-elite/32/34356_2.png) [@C-B-Elite](https://forum.dfinity.org/u/C-B-Elite)
#### Post date: [January 13, 2022, 2:14pm UTC](https://forum.dfinity.org/t/stable-memory-flag/10270/1 "2022-01-13T14:14:39Z")

</div>

how to use the stable memory flag : “–max-stable-pages” ?  
[docs](https://github.com/dfinity/motoko-base/blob/627ecb5a2f54c7b95a5ddd82e32cad3bdb319e6c/src/ExperimentalStableMemory.mo#L32)

---

<div class="post-metadata">

### Author: ![heyuanxun](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/heyuanxun/32/4695_2.png) [@heyuanxun](https://forum.dfinity.org/u/heyuanxun)
#### Post date: [January 13, 2022, 2:15pm UTC](https://forum.dfinity.org/t/stable-memory-flag/10270/2 "2022-01-13T14:15:34Z")

</div>

i really want to know it too

---

<div class="post-metadata">

### Author: ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)
#### Post date: [January 14, 2022, 11:33pm UTC](https://forum.dfinity.org/t/stable-memory-flag/10270/3 "2022-01-14T23:33:23Z")

</div>

The Experimental StableMemory library and motoko stable variables (if any) both share the current 8GB max of IC stable memory available to a canister.

For now , we let the library use at most half this space (65536 \* 64kb = 4GB) to leave space for stable variables. You can allow more or less pages for the library by specifying the command line option. Default is 65536.

---

<div class="post-metadata">

### Author: ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)
#### Post date: [January 15, 2022, 12:01am UTC](https://forum.dfinity.org/t/stable-memory-flag/10270/4 "2022-01-15T00:01:10Z")

</div>

[The ExperimentalStableMemory library :: Internet Computer](https://smartcontracts.org/docs/language-guide/stablememory.html) is an example (not yet linked from site navigation)

---

<div class="post-metadata">

### Author: ![C-B-Elite](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/c-b-elite/32/34356_2.png) [@C-B-Elite](https://forum.dfinity.org/u/C-B-Elite)
#### Post date: [January 15, 2022, 8:47am UTC](https://forum.dfinity.org/t/stable-memory-flag/10270/5 "2022-01-15T08:47:11Z")

</div>

How to specify the flag? I tried it but failed.  
command :  
dfx build --–max-stable-pages 2147483648

For this thread:

> For now , we let the library use at most half this space (65536 \* 64kb = 4GB) to leave space for stable variables. You can allow more or less pages for the library by specifying the command line option. Default is 65536.

If I use the stable memory by using Experimental StableMemory Library and use the default model, the system upgrade functions will also make sense, right?

---

<div class="post-metadata">

### Author: ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)
#### Post date: [January 15, 2022, 10:19am UTC](https://forum.dfinity.org/t/stable-memory-flag/10270/6 "2022-01-15T10:19:14Z")

</div>

The option is an option to the moc compiler, not dfx. For dfx, you can specify moc options in an “args” entry for the canister in dfx.json. Sorry, I don’t have an example to hand.

And yes, you can use both stable variables and ESM.mo in the same canister, including pre/post upgrade methods.

I think the example above even does that if you look carefully.

---

<div class="post-metadata">

### Author: ![C-B-Elite](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/c-b-elite/32/34356_2.png) [@C-B-Elite](https://forum.dfinity.org/u/C-B-Elite)
#### Post date: [January 20, 2022, 8:24am UTC](https://forum.dfinity.org/t/stable-memory-flag/10270/7 "2022-01-20T08:24:03Z")

</div>

Hello claudio, I have a new question about stable memory flag :  
If I set the flag max-page-size to 6G, does the rest of the stable memroy (2G) still can be used for upgrading by using pre/postupgrade functions?

---

<div class="post-metadata">

### Author: ![claudio](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/claudio/32/322_2.png) [@claudio](https://forum.dfinity.org/u/claudio)
#### Post date: [January 20, 2022, 10:41am UTC](https://forum.dfinity.org/t/stable-memory-flag/10270/8 "2022-01-20T10:41:59Z")

</div>

Yes, that’s the intention - the 2 GB will be reserved for storing stable variables during upgrade though they can use more if you haven’t allocated all the 6GB pages available to ExperimentalStableMemory.

To be perfectly honest, I’m not sure I tested this well enough (it was a last minute FR) and should add a test.

To be safe, it might be worth writing a little test program before you rely on this for something important.

I’ll file an issue to test this better. [Tests: test max-stable-mem compiler option properly (unless we do already) · Issue #3065 · dfinity/motoko · GitHub](https://github.com/dfinity/motoko/issues/3065)

---

<div class="post-metadata">

### Author: ![C-B-Elite](https://sea1.discourse-cdn.com/flex023/user_avatar/forum.dfinity.org/c-b-elite/32/34356_2.png) [@C-B-Elite](https://forum.dfinity.org/u/C-B-Elite)
#### Post date: [January 20, 2022, 11:59am UTC](https://forum.dfinity.org/t/stable-memory-flag/10270/9 "2022-01-20T11:59:03Z")

</div>

Ok! That’s really excellent ! I will test the api if I can upgrade my dfx(and moc), Thank you so much for your detail and perfect answer claudio !
