When to use memory vs storage in Solidity

Bradley Woolf

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.

Have an idea that utilises blockchain technology?
We provide end to end solution to create your MVP.
Check out our library of free Smart Contracts for your business
Your go to place for all things NFT & Defi