# evm memory gas costs grow quadratically making large allocations prohibitively expensive
The EVM charges gas for memory based on a quadratic formula: cost increases linearly for small allocations but grows quadratically as memory expands. Memory is never freed or garbage-collected within a transaction — every allocation is permanent for the transaction's lifetime.
This makes large memory operations (such as copying or hashing large arrays) disproportionately expensive and creates an implicit gas limit on data-intensive operations. Since [[unbounded return data from external calls forces quadratic memory cost gas consumption via RETURNDATASIZE]], a malicious callee can weaponize this cost model by returning oversized data that forces the caller into quadratic gas territory. Assembly code that bypasses Solidity's memory manager can trigger unexpected expansion costs if the free memory pointer is not properly maintained.
The quadratic cost model also compounds the impact of [[DoS via block gas limit permanently bricks functions that iterate over unbounded arrays]] — iteration over growing data structures hits both the linear iteration cost and the quadratic memory expansion cost when processing array elements.
---
Relevant Notes:
- [[unbounded return data from external calls forces quadratic memory cost gas consumption via RETURNDATASIZE]] — a direct exploit of quadratic memory costs
- [[DoS via block gas limit permanently bricks functions that iterate over unbounded arrays]] — gas exhaustion is amplified by memory costs
- [[memory-to-memory assignment in solidity creates references not copies enabling aliasing bugs when both variables modify the same data]] — explicit copying to avoid aliasing requires additional memory allocation, amplifying quadratic gas costs
Topics:
- [[evm-internals]]