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).
contract Example {
uint storedData;
function set(uint x) public {
storedData = x; // storage
uint temp = x; // memory
}
}| Step | Action | Variable | Location | Value | Gas Cost Impact |
|---|---|---|---|---|---|
| 1 | Function set called with x=5 | x | Input | 5 | No |
| 2 | Assign x to storedData | storedData | Storage | 5 | Yes (high) |
| 3 | Assign x to temp | temp | Memory | 5 | No (low) |
| 4 | Function ends, temp cleared | temp | Memory | cleared | No |
| 5 | storedData remains | storedData | Storage | 5 | Stored permanently |
| Variable | Start | After Step 2 | After Step 3 | After Step 4 | Final |
|---|---|---|---|---|---|
| x | undefined | 5 | 5 | cleared | cleared |
| storedData | 0 | 5 | 5 | 5 | 5 |
| temp | undefined | undefined | 5 | cleared | cleared |
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