39

What is the difference between Memory and Storage ?

From both the EVM point of view and the contract design one.

Thanks!

3 Answers 3

34

They are analogous to memory and hard drive storage in a computer. The contract can use any amount of memory (as long as it can pay for it of course) during executing its code, but when execution stops, the entire content of the memory is wiped, and the next execution will start fresh. The storage on the other hand is persisted into the blockchain itself, so the next time the contract executes some code, it has access to all the data it previously stored into its storage area.

1
  • The memory of the execution is also stored "into the blockchain itself", as it forms part of past transactions. However, it is not accessible to future executions of the contract directly as storage is.
    – dionyziz
    Mar 27, 2020 at 13:45
35

Memory is temporary. Storage is permanent. For example, you would perform intermediate computations using memory, and then save the result to storage.

Details from the EVM perspective, their structure and gas costs.

Storage is a key/value store where keys and values are both 32 bytes. It is sparse (like a hash table), and there are no inherent gas savings from having two 32 byte values next to each other. Storing one of the values at key 1 and the other at key 1000, costs the same amount of gas as storing them at key 1 and key 2. (Gas savings from packing storage are still possible, such as fitting 2 uint128 values within a single key, instead of using 2 keys.)

Memory is a byte-array. Memory starts off zero-size, but can be expanded in 32-byte chunks by simply accessing or storing memory at indices greater than its current size. Since memory is contiguous, it does save gas to keep it packed and shrink its size, instead of having large patches of zeros. It's cheaper to have an array of length 2 storing 2 values, than an array of length 1000 where the values are at the ends of the array and the middle is all zeros.

The subtleties wiki explains further the gas costs for using memory:

The fee for expanding memory is determined via a subtract-the-integrals method. Specifically, TOTALFEE(SZ) = SZ * 3 + floor(SZ**2 / 512) is the total fee for expanding the memory to SZ 32-byte chunks (note: partially filled chunks are counted, so 33 bytes = 2 chunks), and if a particular operation expands memory from size x to y, the additional gas cost is TOTALFEE(y) - TOTALFEE(x)

Storage gas costs are primarily: 20,000 gas when a value is set to non-zero from zero; 5,000 gas when writing to existing storage or setting a value to zero; and a 15,000 gas refund when a non-zero value is set to zero.

EDIT: EIP-2200 finalized in 2018 has further explanations on storage costs, such as 800 gas for "storing" the same value.

14
  • Can you explain this part in detail Storing one of the values at key 1 and the other at key 1000, costs the same amount of gas as storing them at key 1 and key 2.
    – 11t
    Feb 24, 2017 at 21:06
  • @Rajat I've made edits but not sure how to explain "sparse" further. Is that the issue or what are you looking for?
    – eth
    May 9, 2017 at 6:35
  • actually yes. The sparse is my doubt, but after two months and creating more smart contracts than at that time, I can understand your answer (now) and appreciate the talent of which you have explained. If possible can you direct me to any documentation from where I can learn the things which are going in the background? (Is it the white paper or any other thing)This will help me better appreciate ethereum, and I will be in a position to answer like you with all the technical in simple language as well!
    – 11t
    May 10, 2017 at 7:39
  • 1
    @EralpB It's in the subtleties wiki link, but I have added it to answer now too.
    – eth
    Sep 20, 2017 at 9:21
  • 1
    @NikaKurashvili The part I wrote about memory could be very outdated. I suggest if you can post a new, specific question like ethereum.stackexchange.com/questions/91197/… (or ethereum.stackexchange.com/questions/90318/…) and someone may be able to help. Then feel free to post your own answer here or edit to update this answer. (I already edited this answer to EIP-2200 since I was using an older EIP previously, and it is hard to keep track.)
    – eth
    Jan 29, 2021 at 2:31
0

memory is like RAM of your code, which holds the data in it till the execution of your function, once the function execution is over it is removed.On the other hand storage is like the database which exists in the blockchain independent of the function execution.

1
  • 3
    This adds nothing to the existing, more detailed answers.
    – Chenmunka
    Nov 10, 2017 at 13:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.