The memory
keyword in Solidity is used to denote a temporary storage location within the Ethereum Virtual Machine (EVM) for variables that are not meant to persist between function calls. This is analogous to RAM in a computer, where data stored in memory is temporary and gets erased after the execution of the function it is used in. The EVM has three primary storage locations for data: storage
, memory
, and stack
.
- Storage: This is where all the contract's state variables reside. It is persistent across function calls and is more expensive to use due to the cost of writing to the blockchain.
- Memory: This is used for temporary data. It is cheaper to use than storage because it is erased between function calls, making it suitable for intermediate calculations or temporary data structures.
- Stack: This is used for small local variables and is almost free to use, but it has a limited capacity.
When you declare a variable with the memory
keyword,