40

What is the difference between Memory and Storage ?

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

Thanks!

eth
  • 85,679
  • 53
  • 285
  • 406
Hcharlanes
  • 1,135
  • 1
  • 11
  • 10

3 Answers3

36

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 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.

Iaroslav
  • 301
  • 1
  • 2
  • 19
eth
  • 85,679
  • 53
  • 285
  • 406
  • 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 '17 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 09 '17 at 06: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 '17 at 07:39
  • Overall is one cheaper than the other, if its not that simple can you give a huerestic on deciding which to use – Daniel Kobe Jun 08 '17 at 02:51
  • @DanielKobe Yes, memory is cheaper because it is not permanent. I didn't want to repeat Peter's points but may add a clarification. – eth Jun 11 '17 at 08:29
  • You mentioned storage costs but not memory costs? – EralpB Sep 12 '17 at 08:54
  • 1
    @EralpB It's in the subtleties wiki link, but I have added it to answer now too. – eth Sep 20 '17 at 09:21
  • I think you meant a 20,000 gas refund. I think this makes more sense and also note that setting the value to 0 costs 5,000, netting the 15,000 refund you mentioned. – Nic Szerman Jul 30 '18 at 19:24
  • @NicSzer The numbers may look like they add up, but I think that's more by coincidence and I don't follow your comment. The numbers are from the Yellow Paper. – eth Aug 12 '18 at 02:53
  • Why is it "FLOOR" and not "CEIL"? – dionyziz Mar 27 '20 at 13:47
  • @dionyziz I would guess it's floor because it's typical integer division in most languages. – eth Mar 29 '20 at 14:53
  • @eth Since it explicitly mentions that "partially filled chunks are counted", it seems that this would be counted by using "CEIL" instead of "FLOOR" here. A partially filled chunk would yield a non-integer number after the division, and "CEIL"ing it would lead to the partially filled chunk being counted as a fully filled chunk, which I think is what the fee calculation does. Would that be the correct interpretation? – dionyziz Mar 30 '20 at 09:03
  • @eth I am not following the formula unfortunately. If I put just uint128[1] memory t; t[0] = 10; in my function, the gas cost calling it is 21581. When if I remove those 2 lines from the function(which will be totally empty), now it's 21394. There is like 187 Gas difference. I just used 1-2 slots maximum. Putting 2 in the formula , we get 6. Any idea ? – Nika Kurashvili Jan 20 '21 at 15:55
  • 1
    @NikaKurashvili The part I wrote about memory could be very outdated. I suggest if you can post a new, specific question like https://ethereum.stackexchange.com/questions/91197/sload-costs-800-gas-how-is-it-possible (or https://ethereum.stackexchange.com/questions/90318/does-an-sstore-where-the-new-value-is-the-same-as-the-existing-value-cost-gas) 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 '21 at 02:31
35

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.

eth
  • 85,679
  • 53
  • 285
  • 406
Péter Szilágyi
  • 10,436
  • 39
  • 42
  • 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 '20 at 13:45
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.

cryptoKTM
  • 431
  • 1
  • 3
  • 15