liveBook · Manning

Memory usage

As explained in Advanced Algorithms and Data Structures

Memory consumption is the amount of memory occupied by a computation or program during execution.

Memory usage is the amount of memory occupied by a computation or program during execution. Profiling it reveals whether a data structure's performance advantage carries an unacceptable space cost. section 3.5.4 section 3.5.3

Compare memory requirements by separating container size, stored data, and per-element overhead:

  • Trees storing n keys still require n nodes whether they are balanced or skewed when they do not support compression. section 3.5.4
  • Randomized treaps use slightly more than twice the memory of binary search trees for integer keys because each node stores an additional priority. section 3.5.4
  • The ratio can shrink to about 1.25 for four- to ten-character strings, showing that key representation affects the practical cost. section 3.5.4
  • For generic strings, estimate storage from key lengths: average length L gives space proportional to n*L, while maximum length m gives O(n*m). section 6.1.1

Choosing a structure for stored words

A trie can save roughly 30% of storage compared with a binary search tree when words share prefixes, but a degenerate trie can store information inefficiently. For a static set, a hash table may provide fast lookups; a Bloom filter can trade memory savings for reduced precision. section 6.2.1 section 6.1.1

Figure 3.23 Cumulative memory allocation for BST and RandomizedTreap, in the most generic case, with random insertions and removals

Compare cumulative memory allocation for binary search trees and randomized treaps across insertions and removals. section 3.5.4

Figure 6.4 Comparing BST and trie approaches on a different example. When the keys in the trie have a larger ratio of shared characters (that is, a longer common prefix), tries are more efficient in storing strings

Inspect how shared character prefixes change trie storage efficiency relative to binary search trees. section 6.2.1

How Advanced Algorithms and Data Structures covers it

How deep it goes
Develops node-count reasoning, key-size formulas, profiling comparisons, and structural trade-offs without detailing allocator internals.
Examples
Compares BST and treap allocations for integer and string keys, and contrasts trie storage when strings share prefixes or do not.
What it assumes
Assumes familiarity with trees and dictionary operations, while contrasting constant-size keys with variable-length strings.
What you can do after
Evaluate data-structure choices by estimating space from node counts, key lengths, overhead, and measured allocations.
How it connects
Connects memory usage with profiling, binary search trees, randomized treaps, tries, dictionaries, hash tables, and Bloom filters.

Also explained in

Compare how 7 books explain memory usage →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Advanced Algorithms and Data Structures. The section links point to the parts of the book it draws on.