0
0
Blockchain / Solidityprogramming~10 mins

Storage vs memory usage in Blockchain / Solidity - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Storage vs memory usage
Start Transaction
Check Data Type
Storage
Write to Disk
Cost Gas
End Transaction
This flow shows how blockchain handles data: deciding if data goes to storage (permanent, costly) or memory (temporary, cheaper).
Execution Sample
Blockchain / Solidity
contract Example {
  uint storedData;
  function set(uint x) public {
    storedData = x; // storage
    uint temp = x;  // memory
  }
}
This smart contract stores a number permanently and uses a temporary memory variable inside a function.
Execution Table
StepActionVariableLocationValueGas Cost Impact
1Function set called with x=5xInput5No
2Assign x to storedDatastoredDataStorage5Yes (high)
3Assign x to temptempMemory5No (low)
4Function ends, temp clearedtempMemoryclearedNo
5storedData remainsstoredDataStorage5Stored permanently
💡 Function ends, memory variables cleared, storage variables remain with gas cost paid
Variable Tracker
VariableStartAfter Step 2After Step 3After Step 4Final
xundefined55clearedcleared
storedData05555
tempundefinedundefined5clearedcleared
Key Moments - 3 Insights
Why does storedData cost gas but temp does not?
Because storedData is saved in storage which is permanent and costly (see step 2 in execution_table), while temp is in memory and temporary (step 3 and cleared at step 4).
What happens to memory variables after function ends?
Memory variables like temp are cleared after the function finishes (step 4), so they do not persist.
Can storage variables be changed without gas cost?
No, changing storage variables costs gas because it writes to the blockchain permanently (step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of storedData after step 3?
Acleared
Bundefined
C5
D0
💡 Hint
Check the 'storedData' row after step 3 in variable_tracker.
At which step does the memory variable temp get cleared?
AStep 2
BStep 4
CStep 3
DStep 5
💡 Hint
Look at the 'temp' variable value change in execution_table and variable_tracker.
If storedData was assigned inside memory instead of storage, what would happen to gas cost?
AGas cost would be zero
BGas cost would increase
CGas cost would stay the same
DGas cost would be unpredictable
💡 Hint
Refer to gas cost impact in execution_table for storage vs memory assignments.
Concept Snapshot
Storage vs Memory in blockchain:
- Storage: permanent, costly, saved on blockchain
- Memory: temporary, cheap, used during function execution
- Writing to storage costs gas
- Memory cleared after function ends
- Use storage for data to keep, memory for temporary calculations
Full Transcript
This visual execution shows how blockchain smart contracts handle storage and memory. When a function runs, input variables are received. Assigning a value to a storage variable saves it permanently on the blockchain and costs gas. Assigning to a memory variable uses temporary RAM and costs little or no gas. After the function finishes, memory variables are cleared, but storage variables remain. This distinction is important because storage is expensive and permanent, while memory is cheap and temporary. The execution table traces each step, showing variable values and gas cost impact. Key moments clarify why storage costs gas and memory does not, and what happens to variables after function ends. The quiz tests understanding of variable states and gas costs. The snapshot summarizes the main points for quick review.