# Ethereum Storage and Memory

{% hint style="info" %}
[**Book an audit with Zokyo**](https://www.zokyo.io/)
{% endhint %}

{% hint style="info" %}
Ethereum's robust architecture is the foundation for many sophisticated contract structures, including proxy contracts. A crucial component in creating and understanding proxy contracts is how Ethereum manages data. Let's lay down the fundamentals of Ethereum's "Storage" and "Memory," which play pivotal roles in the design and functionality of proxy contracts.
{% endhint %}

### **Storage: The Permanent Record Keeper**

Every smart contract in Ethereum comes with its own set of long-term memory. This is what we refer to as "Storage." It's where data that needs to persist, or remain unchanged, is kept.

* **Persistent Data**: The term "persistent" simply means the data doesn't go away. It's like saving a file on your computer's hard drive. Even if you turn off the computer, the file is still there when you turn it back on.
* **Structure & Costs**: Think of storage as a vast filing cabinet. Each drawer or compartment has a label and can store a piece of information. However, writing data into this filing cabinet (or updating it) requires energy, or in Ethereum's terms, "gas." Because space on the blockchain is precious, there's a cost associated with keeping data here.
* **Differentiating Storage**: Ethereum also has short-term data spaces like the stack and memory. They're like scribbling something on a piece of paper and then throwing it away. But storage? It's a long-term commitment, keeping data safe and unchanged.

### **Memory: The Brief Note-Taker**

In contrast to the permanence of storage, there's "Memory" — Ethereum's transient data holder, existing only for a contract's short-lived operations.

* **Life Cycle**: When a contract is executed, memory is there as a scratchpad. It begins empty, captures interim data during execution, and then clears out once the operation concludes.
* **Cost Dynamics**: Memory usage incurs gas costs too. Yet, it's typically more economical for temporary computations than storage, as long as we remember its fleeting nature.
* **Its Role in Proxy Contracts**: Memory provides temporary workspaces for processes and doesn't directly impact the storage structure. But understanding it aids in appreciating the distinction between short-term and persistent data, a crucial concept when diving deeper into proxy contracts.

### **Storage Slots: The Organized Shelves of Storage**

Now, within the vast realm of Ethereum's storage, data doesn't just float around. It's systematically placed in 'storage slots'.

* **Defining Storage Slots**: Within the expansive storage filing cabinet, each unique compartment, or slot, houses a particular piece of data.
* **Organizing Data**: Data begins at slot `0`, with subsequent data going into slot `1`, and so on. For intricate data structures like mappings, Ethereum uses a method (Keccak256 hash function) to determine its rightful slot.



<figure><img src="https://2435636835-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2F5BID0P7sbZpVTEKkwMbp%2Fuploads%2FYiy4zLXvONN2aLwY0W3u%2FStorage%20slots.png?alt=media&#x26;token=a5658fb0-d729-4140-9966-a6b6b26c86bf" alt=""><figcaption></figcaption></figure>

* **The Proxy Contract Connection**: This is where it gets crucial for proxy contracts. These contracts utilize a mechanism called delegate calls. If not managed accurately, delegate calls can unintentionally jumble up storage slots. It's akin to misfiling documents in a cabinet. Misplaced data within proxy contracts can lead to malfunctioning operations. Thus, having a clear mapping of storage slots is indispensable when crafting or interacting with proxy contracts.
