androidengineers.Book a session

Foundations of Data Structures

Static vs Dynamic Data Structures

article20 minEasy

One of the first architectural decisions when modeling data is choosing between static and dynamic allocation. This choice dictates how memory is reserved, whether size can change at runtime, and how efficiently the CPU can process items.


What is a Static Data Structure?

A static data structure has a fixed size determined at allocation time (either compile time or during runtime initialization). Once allocated, its memory capacity cannot expand or shrink.

// Fixed array of size 5 allocated in memory
val scores = IntArray(5)

Memory Characteristics of Static Structures

  • Allocated as a single, contiguous block of bytes.
  • Size must be known beforehand.
  • No dynamic memory reallocation overhead during operations.
  • Risk of buffer overflow (if data exceeds capacity) or wasted RAM (if oversized).
Static Array in Memory:
[ Index 0 ][ Index 1 ][ Index 2 ][ Index 3 ][ Index 4 ]
  0x1000     0x1004     0x1008     0x100C     0x1010

What is a Dynamic Data Structure?

A dynamic data structure can expand and contract at runtime as elements are inserted or removed. It does not require a fixed upfront capacity.

Examples include:

  • Node-based collections: Linked Lists, Trees, Graphs.
  • Resizing array wrappers: Kotlin ArrayList, Java Vector, C++ std::vector.
// Dynamically sized list that allocates as needed
val dynamicList = ArrayList<String>()
dynamicList.add("User 1")
dynamicList.add("User 2")

Memory Characteristics of Dynamic Structures

  • Linked nodes: Stored in non-contiguous heap memory, chained via pointers/references.
  • Dynamic arrays: Underlying contiguous array is reallocated with a growth factor (e.g., 1.5x or 2x) when full.

Comprehensive Comparison Matrix

PropertyStatic Data Structures (e.g. Fixed Array)Dynamic Data Structures (e.g. LinkedList, ArrayList)
SizeFixed at allocation timeFlexible; grows and shrinks at runtime
Memory AllocationTypically Stack or single continuous Heap blockHeap memory allocated per node or dynamically resized
Access TimeO(1) constant time random accessO(1) for dynamic arrays, O(n) for linked lists
Insertion / DeletionInefficient (O(n) shift) or impossibleEfficient (O(1) at head/tail for linked nodes)
Memory OverheadZero pointer overhead; minimal metadataHigh: 4-8 bytes per pointer + object headers
Cache LocalityExcellent (consecutive cache lines)Poor in linked structures (pointer hopping)

When to Choose Which?

Use Static Data Structures when:

  1. The maximum number of elements is strictly known in advance (e.g., days of week, 12 months, fixed size lookup tables).
  2. Memory constraints are critical (e.g., embedded systems, high-frequency audio DSP buffers, gaming engines).
  3. Maximum throughput and CPU cache hits are required.

Use Dynamic Data Structures when:

  1. The volume of data is unpredictable (e.g., user search queries, incoming network packets).
  2. Frequent insertions and deletions occur throughout the lifecycle of the collection.
  3. You want convenient high-level APIs without manually managing buffer growth.

Summary

  • Static structures have fixed bounds, superior cache locality, and zero reference overhead, but lack flexibility.
  • Dynamic structures adapt to fluctuating workloads at the expense of pointer memory overhead or reallocation penalties.

YOUR LEARNING JOURNEY

0 of 55 available lessons completed

Progress saved in this browser. No account needed.
Static vs Dynamic Data Structures | Data Structures | Android Engineers