Difference between revisions of "GHC/Memory Footprint"

From HaskellWiki
< GHC
Jump to navigation Jump to search
Line 73: Line 73:
 
| [v]
 
| [v]
 
| (''1'' + 3N) words + N * sizeof(v)
 
| (''1'' + 3N) words + N * sizeof(v)
  +
| single shared []
| [] is singleton
 
 
|-
 
|-
 
| Data.ByteString
 
| Data.ByteString
Line 95: Line 95:
 
| Data.HashMap k v
 
| Data.HashMap k v
 
| 4.5N words + N * (sizeof(k) + sizeof(v))
 
| 4.5N words + N * (sizeof(k) + sizeof(v))
  +
|
| non-HAMT impl.
 
 
|-
 
|-
 
| Data.HashSet
 
| Data.HashSet

Revision as of 01:16, 18 November 2012

This page is concerned with the memory footprint of Haskell data structures stored in the heap.

The heap is the garbage collected area of memory in which the running program introduces heap nodes.

An in-depth explanation of the GHC internals can be found in the GHC Commentary: The Layout of Heap Objects.

A good introduction on how to compute the size of Haskell datastructures can be found in Johan Tibell's Computing the size of a HashMap.

Memory Footprints of common data types

See also Memory footprints of some common data types which is the origin of the table below.

The following tables assumes fully evaluated data structures (i.e. no thunks)

A "word" is 4 bytes on 32bit archs, and 8 bytes on 64bit archs. Sizes are usually rounded up to word-boundaries.

Constructors with no fields are instantiated only once on the heap. This is expressed in the sizeof()-formulas below with italic numbers which can be ignored for practical considerations.

Basic Types

Data type sizeof(T) Notes
() 1 word single shared ()
Bool 1 word single shared True/False
Char 2 words Char-sharing pool
Int 2 words
Int64 (on 64bit arch) 2 words
Int64 (on 32bit arch) 3 words
Double (on 64bit arch) 2 words
Double (on 32bit arch) 3 words
Integer (small) 2 words
Integer (bignum rep.) 3 words + sizeof(bignum-repr) FIXME

Container Types

Data type sizeof(T) Notes
(va,vb) 3 words + sizeof(va) + sizeof(vb)
[v] (1 + 3N) words + N * sizeof(v) single shared []
Data.ByteString 9 words + N bytes
Data.Text 6 words + 2N bytes
Data.Map k v 6N words + N * (sizeof(k) + sizeof(v))
Data.Set v 5N words + N * sizeof(v)
Data.IntMap v (3N + 5(N-1) words) + sizeof(v)
Data.IntSet (2N + 5(N-1)) words
Data.HashMap k v 4.5N words + N * (sizeof(k) + sizeof(v))
Data.HashSet 4.5N words + N * sizeof(v)
Data.Vector v (4 + (2+N)) words + N * sizeof(v) O(1) slicing shares Array#