Get in Touch
Thank you! Your submission has been received!
Oops! Something went wrong while submitting the form.
twitter logodiscord logo
close icon

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.

Want to accept all ERC20's and network tokens?
Get in touch