Does the EVM always allocate new memory or can it reuse already allocated, but unused, memory?

Reading the docs for delete (http://solidity.readthedocs.io/en/develop/types.html), it is mentioned that:

delete a assigns the initial value for the type to a. I.e. for integers it is equivalent to a = 0, but it can also be used on arrays, where it assigns a dynamic array of length zero or a static array of the same length with all elements reset. For structs, it assigns a struct with all members reset.

Does this mean that if a is a dynamic array (uint[]) of, say, length n, then delete a assigns to a an empty array:a = new uint[](0)? Not the original array, of length n, with its contents set to 0?

So, if new and delete are used many times in a Solidity contract, will the EVM keep allocating new memory (expanding memory), or will it reuse already allocated memory when possible?

Consider the loop:

uint[] a;
for (uint i = 0; i < 1000; ++i) {
   a = new uint[](100);
   delete a;

Will this allocate 100 or 100000 memory cells?

1 Answer 1


This is an interesting question, so I wrote some quick code to test it out.

contract Memsize {
    function foo(uint _its) pure public returns (uint) {
        uint ms;
        uint[] memory a;
        for (uint i = 0; i < _its; ++i) {
            a = new uint[](100);
            delete a;
            ms := msize()


foo(1) 3392
foo(2) 6656
foo(3) 9920

Etc. So, no the memory is not re-used by Solidity and is allocated fresh each time.

This is not a limitation of the EVM. There's no reason why the memory couldn't be re-used. It is a property of the Solidity compiler.

If you really care about this stuff (efficiency optimisation of smart contracts), then you may be interested in LLL as a contract language. Some docs here.

  • Thanks, once again for an excellent answer. I didn't knew how to test it, so thanks for your code.
    – Shuzheng
    Nov 30, 2017 at 11:04

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.