Gas Fundamentals
Gas is the meter that makes a shared computer safe to use. This lesson explains what gas measures, how gas limits protect senders and the network, what happens when a transaction runs out of gas, and how block and transaction limits constrain contracts.
Learning outcomes
After completing this lesson, you will be able to:
- Explain what gas measures and why it exists.
- Explain what happens when a transaction runs out of gas.
- Describe block and transaction gas limits.
- Distinguish gas used from the price of gas.
What gas measures
Gas is the unit of computational work on Ethereum. Every EVM opcode has a gas cost that reflects the resources it consumes from every node: adding two numbers costs 3 gas, reading a storage slot for the first time costs 2,100, and writing a new storage value costs 20,000 or more. A transaction's total gas is the sum of an intrinsic cost and the cost of everything it executes.
Gas is separate from the price of ether. The amount of gas an operation uses is fixed by the protocol, while the price per unit of gas, quoted in gwei, varies with demand for block space. The fee in ether is gas used multiplied by the gas price, so the same transaction costs more ether when the network is busy.
Gas used or gas price?
Gas limits
Every transaction sets a gas limit: the most gas it may consume. The limit protects the sender from paying for unexpectedly expensive or endless execution, and it protects the network from infinite loops, which simply run out of gas. The following sequence shows what happens to the gas of a transaction.
-
Reserve
Before execution, the sender's balance must cover gas limit × max fee per gas plus any value sent.
-
Execute
Each opcode consumes gas from the limit.
-
Succeed
If execution completes, the sender pays for the gas used; unused gas is not charged.
-
Fail
If execution reverts or runs out of gas, its state changes are undone, but the gas used is still paid.
Wallets and libraries set the limit from an estimate plus a margin. A limit that is too low makes the transaction fail with an out-of-gas error and lose its fee; a generous limit costs nothing extra, because only gas used is charged.
Setting a gas limit
Block and transaction limits
Each block has a block gas limit that caps the total gas of all its transactions. Validators adjust it gradually; it stayed at 30 million for years and has been raised several times since 2025. Since the Fusaka upgrade in December 2025, a single transaction may use at most 16,777,216 (224) gas, whatever the block limit is (EIP-7825).
These limits shape contract design. A function whose gas grows with the size of a list, such as a loop over all users, will eventually exceed the transaction cap and become impossible to execute, a denial-of-service risk covered in the security modules.
21,000gas
Minimum cost of any transaction
16,777,216gas
Maximum gas of one transaction (Fusaka)
10⁹wei
One gwei, the usual gas price unit
10¹⁸wei
One ether
Limits on gas
Free reads
Gas is charged only to transactions that are included in blocks. Calling a view function from an application with eth_call runs on one node, changes nothing and costs nothing, although the node still meters gas internally and enforces a limit. This is why DApps read freely and pay only to write.
Out of gas
A user sends a transaction with a gas limit of 50,000 to a function that needs about 70,000. What happens to the state change and to the user's ether?
Execution stops with an out-of-gas error when the 50,000 gas is exhausted. All state changes are reverted, so the function has no effect, but the transaction is included in a block and the user pays for all 50,000 gas. Retrying with a limit of, say, 90,000 succeeds and charges only the roughly 70,000 gas actually used.
Gas versus price
On Monday a token transfer uses 51,000 gas and costs 0.000255 ETH. On Tuesday the same transfer uses 51,000 gas and costs 0.00153 ETH. What changed?
The gas used is the same, so the price per gas changed: 0.000255 ETH ÷ 51,000 = 5 gwei on Monday and 0.00153 ETH ÷ 51,000 = 30 gwei on Tuesday, reflecting higher demand for block space. The contract did not become more expensive to run; the market price of block space rose.
A transaction runs out of gas. Are its state changes kept, and is the gas paid?
Show answer
No state changes are kept; everything is reverted. The gas consumed up to the failure, which is the whole gas limit, is still paid.
Exam Tips
Remember for the exam
- Gas measures work and is fixed per operation; the gas price in gwei varies with demand.
- Unused gas is not charged; gas used before a revert or out-of-gas failure is.
- One transaction may use at most 16,777,216 gas since Fusaka; blocks have their own limit.
- Calling view functions off chain is free.