Storage vs Memory in Solidity

Storage and Memory keywords in Solidity are analogous to Computer’s hard drive and Computer’s RAM. Much like RAM, Memory in Solidity is a temporary place to store data whereas Storage holds data between function calls. The Solidity Smart Contract can use any amount of memory during the execution but once the execution stops, the Memory is completely wiped off for the next execution. Whereas Storage on the other hand is persistent, each execution of the Smart contract has access to the data previously stored on the storage area.

Every transaction on Ethereum Virtual Machine costs us some amount of Gas. The lower the Gas consumption the better is your Solidity code. The Gas consumption of Memory is not very significant as compared to the gas consumption of Storage. Therefore, it is always better to use Memory for intermediate calculations and store the final result in Storage.

  1. State variables and Local Variables of structs, array are always stored in storage by default.
  2. Function arguments are in memory.
  3. Whenever a new instance of an array is created using the keyword ‘memory’, a new copy of that variable is created. Changing the array value of the new instance does not affect the original array.

Example#1: In the below example, a contract is created to demonstrate the ‘storage’ keyword.

Solidity

filter_none

edit
close

play_arrow

link
brightness_4
code

pragma solidity ^0.4.17;
  
// Creating a contract
contract helloGeeks 
{
  // Initialising array numbers
  int[] public numbers;
  
  // Function to insert values
  // in the array numbers
  function Numbers() public 
  {
    numbers.push(1);
    numbers.push(2);
  
    //Creating a new instance
    int[] storage myArray = numbers;
      
    // Adding value to the 
    // first index of the new Instance
    myArray[0] = 0;
  }  
}

chevron_right


Output:

When we retrieve the value of the array numbers in the above code, Note that the output of the array is [0,2] and not [1,2]



This can be understood from the following diagram –

keyword storage

Using the keyword “storage”

Example#2: In the below example, a contract is created to demonstrate the keyword ‘memory’.

Solidity

filter_none

edit
close

play_arrow

link
brightness_4
code

pragma solidity ^0.4.17;
  
// Creating a contract
contract helloGeeks 
{  
  // Initialising array numbers
  int[] public numbers;
    
  // Function to insert
  // values in the array
  // numbers
  function Numbers() public 
  {
    numbers.push(1);
    numbers.push(2);
      
    //creating a new instance
    int[] memory myArray = numbers;
      
    // Adding value to the first
    // index of the arrat myArray
    myArray[0] = 0;
  }  
}

chevron_right


Output:

When we retrieve the value of the array numbers in the above code, Note that the output of the array is [1,2]. In this case, changing the value of myArray does not affect the value in the array numbers.

This can be understood from the following diagram –

keyword memory

Using the keyword “memory”




My Personal Notes arrow_drop_up

Better to be a Geek than an idiot Assimilating the Knowledge around

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :

Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.