Sub topic 8
Memory Overhead per Element
This chapter assumes you know basic Python, understand references or pointers, and can read O(1) and O(n) notation. It covers how much memory each structure spends on things other than your data, and when that cost matters.
What "overhead" means
Overhead is every byte a structure uses that is not the payload you wanted to store. Two sources matter here:
- Bookkeeping per element. Pointers or object headers attached to each item.
- Slack space. Capacity that is reserved but not yet used.
Arrays and linked lists pay these costs in different places.
Overhead in a linked list
Every node stores its value plus at least one reference to the next node. A doubly linked node stores two references. In a compiled language such as C, with 8-byte pointers on a 64-bit machine, storing a 4-byte integer in a singly linked node looks like this:
| Layout | Payload | Pointers | Padding | Total per element |
|---|---|---|---|---|
| Array of `int32` | 4 bytes | 0 | 0 | 4 bytes |
| Singly linked node | 4 bytes | 8 bytes | 4 bytes | 16 bytes |
| Doubly linked node | 4 bytes | 16 bytes | 4 bytes | 24 bytes |
Padding appears because the compiler aligns the 8-byte pointer to an 8-byte boundary. For small payloads, the node is several times larger than the data.
Allocators add more. Each node is a separate heap allocation, and most allocators attach a small header and round the size up. In practice a 16-byte node often occupies 32 bytes. This is why linked lists of small values are especially wasteful.
Overhead in a dynamic array
A dynamic array has no per-element pointers. Its overhead is slack. With the doubling strategy from the previous chapter, the buffer is between 50% and 100% full right after a resize, so up to half of the allocated space can be empty.
During a resize the old and new buffers exist at the same time. For a moment the array needs about 3 times its current element storage (old buffer of n, new buffer of 2n). This peak can matter on memory-constrained systems.
A Python-specific caveat
Python changes the picture. A Python list stores references to objects, not the values themselves. Each slot is an 8-byte pointer, and each integer or string lives in its own object elsewhere. So a Python list pays a pointer per element, just as a linked list does.
You can measure the container cost directly:
import sys
class Node:
__slots__ = ("value", "next")
def __init__(self, value, next=None):
self.value = value
self.next = next
n = 1000
items = list(range(n))
head = None
for i in reversed(range(n)):
head = Node(i, head)
list_bytes = sys.getsizeof(items)
node_bytes = n * sys.getsizeof(Node(0))
print(f"list container: {list_bytes} bytes")
print(f"{n} nodes: {node_bytes} bytes")
print(f"nodes per list: {node_bytes / list_bytes:.1f}x")sys.getsizeof reports only the container's own size, not the integers it refers to. Both versions share the same integer objects, so the comparison isolates the structural cost. Exact numbers vary by Python version, but the node chain is consistently several times larger than the list's pointer buffer.
For compact numeric storage in Python, use array.array or NumPy. They store raw values with no per-element object.
from array import array
compact = array("i", range(1000)) # 4 bytes per element, no per-item objects
print(compact.itemsize * len(compact)) # 4000Comparing the two costs
The two costs scale differently. Array slack is a fraction of the total, so it grows with n but stays a bounded percentage. Linked list overhead is a fixed amount per element, which is a large percentage when the payload is small and a small percentage when the payload is large.
When overhead decides the choice
- Small payloads, many elements. Millions of integers or booleans strongly favor an array. A linked list can use 4 to 8 times more memory.
- Large payloads. If each element is a 2 KB record, a 16-byte pointer is under 1% of the total. Overhead stops being a useful reason to choose either structure.
- Unknown final size. A linked list wastes nothing on unused capacity. If you know the size ahead, preallocate the array and the slack disappears. If you do not, a dynamic array's slack is usually still cheaper than per-node cost.
- Tight peak memory. The temporary double buffer during resize can push a program over its limit. Reserve capacity up front when you can.
Summary of the trade
Arrays pay in slack and a brief resize spike. Linked lists pay a fixed tax on every element. For small elements the linked list tax dominates, so arrays are usually the smaller choice.
Exercise
A C program stores 10 million 4-byte integers. A singly linked node takes 16 bytes (4 payload, 4 padding, 8 pointer), and a dynamic array may be up to 50% empty. Roughly how much memory does each use in the worst case, and which is smaller?
Show answer
The linked list uses about 10,000,000 × 16 = 160 MB (more with allocator headers). The array holds 40 MB of data; at worst it is half empty, so about 80 MB of allocated capacity. The array is smaller by about a factor of two even in its worst case. During a resize the peak can reach roughly 3 times the data, about 120 MB, which is still below the linked list.
Exercise
Why does a Python list of integers not save much memory compared with a linked list of Node objects holding the same integers, and what would you use instead for compact storage?
Show answer
A Python list stores an 8-byte reference per element, and each integer is a separate object, so it pays a pointer per item like a linked list does. Node objects add their own object size on top of that, so the list is still smaller, but the integer objects dominate both. For compact storage use array.array or NumPy, which store raw values contiguously with no per-element object.