When to use memory vs storage in Solidity

The definition

Storage: variables that are stored on the blockchain forever (think hard drive)

Memory: variables that are stored between function calls, and are erased after external function calls (think RAM)

When do decide what to use?

When using structs and arrays within functions in Solidity, you need to decide which keyword to use. The good news, however, is that the compiler provides feedback to the developer at compilation whether they chose correctly.

The main question you need to ask yourself is: do I need this variable outside of this function execution environment? If so, use storage.

Note: there are actually three forms of memory in the EVM- storage, memory, and the call-stack. This means that you can store very short-lived variables in the actual call stack.

Tired of doing the same activities over and over again?

Get Started