# Does the MakeCode compiler leave constants in Flash memory to save RAM?

**URL:** <https://forum.makecode.com/t/does-the-makecode-compiler-leave-constants-in-flash-memory-to-save-ram/1103>\
**Category:** Arcade\
**Created:** [January 22, 2020, 10:50pm UTC](https://forum.makecode.com/t/does-the-makecode-compiler-leave-constants-in-flash-memory-to-save-ram/1103 "2020-01-22T22:50:28Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![kjw](https://avatars.discourse-cdn.com/v4/letter/k/e79b87/32.png) [@kjw](https://forum.makecode.com/u/kjw)\
**Post date:** [January 22, 2020, 10:50pm UTC](https://forum.makecode.com/t/does-the-makecode-compiler-leave-constants-in-flash-memory-to-save-ram/1103/1 "2020-01-22T22:50:28Z")

</div>

When MakeCode is compiled and there are things like strings which do not change, are these left in flash memory and referenced from there to reduce RAM usage? I’m thinking along the lines of Arduino/C’s [PROGMEM](https://www.arduino.cc/reference/en/language/variables/utilities/progmem/) but with the compiler figuring that out for itself.

[Added later]. Images in MakeCode Arcade are the more obvious data type for this treatment.

---

<div class="post-metadata">

**Author:** ![jwunderl](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jwunderl/32/5308_2.png) [@jwunderl](https://forum.makecode.com/u/jwunderl)\
**Post date:** [January 23, 2020, 12:16am UTC](https://forum.makecode.com/t/does-the-makecode-compiler-leave-constants-in-flash-memory-to-save-ram/1103/2 "2020-01-23T00:16:04Z")

</div>

any image that is not created / edited programmatically (e.g. including `img` tagged templates) is stored in flash and brought over to memory only when it is editted (e.g. if you set pixels to the image at runtime), and we now have [constant folding](https://github.com/microsoft/pxt/pull/5917) which inlines simple number operations / etc where possible (which is why infinity / nan were added in that pr, as it removed the memory allocation)

---

<div class="post-metadata">

**Author:** ![mmoskal](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/mmoskal/32/20_2.png) [@mmoskal](https://forum.makecode.com/u/mmoskal)\
**Post date:** [January 23, 2020, 6:30am UTC](https://forum.makecode.com/t/does-the-makecode-compiler-leave-constants-in-flash-memory-to-save-ram/1103/3 "2020-01-23T06:30:36Z")

</div>

The other thing that’s kept in flash is hex literals. They can be used to store constant arrays (with a bit of encoding).

OTOH Const Foo = [1,2,3,…] generates rather inefficient code and takes RAM.

---

<div class="post-metadata">

**Author:** ![kjw](https://avatars.discourse-cdn.com/v4/letter/k/e79b87/32.png) [@kjw](https://forum.makecode.com/u/kjw)\
**Post date:** [February 5, 2020, 6:24pm UTC](https://forum.makecode.com/t/does-the-makecode-compiler-leave-constants-in-flash-memory-to-save-ram/1103/4 "2020-02-05T18:24:18Z")

</div>

A common technique for reversing sprites images is to code it rather than have a second sprite image particularly given the lack of a feature to flip the image in sprite editor. What does the block `flip picture horizontally` (`flipY` method) do to the image? It sounds like it would be in RAM after that?

---

<div class="post-metadata">

**Author:** ![kjw](https://avatars.discourse-cdn.com/v4/letter/k/e79b87/32.png) [@kjw](https://forum.makecode.com/u/kjw)\
**Post date:** [February 5, 2020, 6:27pm UTC](https://forum.makecode.com/t/does-the-makecode-compiler-leave-constants-in-flash-memory-to-save-ram/1103/5 "2020-02-05T18:27:52Z")

</div>

What would the technique described in [Best practice making levels with the new tilemap editor](https://forum.makecode.com/t/best-practice-making-levels-with-the-new-tilemap-editor/1133) do with the levels in the array, would they end up in RAM?

I’ve been using a big `if` statement for level switching which that OP describes as inelegant but maybe I’ve minimised the amount of RAM usage (by luck)?

---

<div class="post-metadata">

**Author:** ![mmoskal](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/mmoskal/32/20_2.png) [@mmoskal](https://forum.makecode.com/u/mmoskal)\
**Post date:** [February 5, 2020, 6:42pm UTC](https://forum.makecode.com/t/does-the-makecode-compiler-leave-constants-in-flash-memory-to-save-ram/1103/6 "2020-02-05T18:42:19Z")

</div>

All tile maps sit in flash as far as I know. You may have an array but it’s an array of pointers so it doesn’t take much ram.

---

<div class="post-metadata">

**Author:** ![jwunderl](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.makecode.com/jwunderl/32/5308_2.png) [@jwunderl](https://forum.makecode.com/u/jwunderl)\
**Post date:** [February 5, 2020, 7:20pm UTC](https://forum.makecode.com/t/does-the-makecode-compiler-leave-constants-in-flash-memory-to-save-ram/1103/7 "2020-02-05T19:20:19Z")

</div>

That will create the image at runtime, so yes, flipX and flipY will take up space in memory
