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.