The EVM can reclaim storage by using the ‘delete’ keyword in Solidity.

However, memory expands when more is allocated by the ‘new’ keyword, right? Are local variables also stored in memory?

When does memory shrinks, so that memory doesn’t keep expanding?

1 Answer 1


No. The memory use during a single EVM execution/contract execution only ever increases.

Equivalently, you get charged gas for increasing the amount of memory in use, but you don't get a refund for decreasing the amount. As you say, this is different from storage.

In terms of the Yellow Paper, the memory size in words is mu_i. Whenever this is re-calculated, it is always in terms of max(mu_i, current_memory_access) - so it is monotonic; it never goes down.

Even more interestingly, just reading memory (not writing to it) can increase the amount of memory in use, mu_i. So you will be charged for expanding the memory if you just read beyond the current top of memory. See the definition of MLOAD in the Yellow Paper.

Solidity will mostly store local variables in memory, but it may sometimes use the stack for intermediate values.

  • Thanks for an excellent answer. Is there any way to deduce whether memory or the stack is used for storing local variable in Solidity, other than looking at byte code? Does delete also apply to memory variables, i.e., can it be used to set memory to "zero"?
    – Shuzheng
    Nov 30, 2017 at 9:44
  • There is some info on stack vs. memory storage in the Solidity docs. delete does work on memory variables; it just sets it to zero. Nov 30, 2017 at 9:51
  • Thanks again. Could you answer my other question if you've the time? ethereum.stackexchange.com/questions/32096/…
    – Shuzheng
    Nov 30, 2017 at 9:57

Your Answer

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

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