0PricingLogin
Web3 & DApp Development Fundamentals · Lesson

Storage vs Memory

Data location.

Why Data Location Matters

In Solidity, reference types must specify a data location. The three locations are storage, memory, and calldata.

Choosing the right one affects persistence, mutability, and most importantly gas cost.

Storage

Storage is the contract's permanent on-chain state. State variables live here, and writing to storage is the most expensive operation.

Data in storage persists between transactions.

<code>contract Bank {
    uint256 public total; // lives in storage forever

    function add(uint256 v) public {
        total += v; // writes to storage (expensive)
    }
}</code>

All lessons in this course

  1. Value Types
  2. Reference Types
  3. Storage vs Memory
  4. Constants and Immutables
← Back to Web3 & DApp Development Fundamentals