Personal tools

GHC/Memory Footprint

From HaskellWiki

< GHC(Difference between revisions)
Jump to: navigation, search
(Basic Types)
(Container Types)
 
(5 intermediate revisions by one user not shown)
Line 11: Line 11:
 
See also [http://blog.johantibell.com/2011/06/memory-footprints-of-some-common-data.html Memory footprints of some common data types] which is the origin of the table below.
 
See also [http://blog.johantibell.com/2011/06/memory-footprints-of-some-common-data.html 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)
+
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.
+
A "word" is 4 bytes on 32=bit architectures, and 8 bytes on 64-bit architectures. 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 ===
 
=== Basic Types ===
Line 25: Line 25:
 
| ()
 
| ()
 
| 0 words
 
| 0 words
| single shared ()
+
| Single shared ()
 
|-
 
|-
 
| Bool
 
| Bool
 
| 0 words
 
| 0 words
| single shared True/False
+
| Single shared True/False
 
|-
 
|-
 
| Char
 
| Char
Line 37: Line 37:
 
| Int
 
| Int
 
| 2 words
 
| 2 words
| Due to alignment
+
|
 
|-
 
|-
 
| Int8
 
| Int8
Line 51: Line 51:
 
| Due to alignment
 
| Due to alignment
 
|-
 
|-
| Int64 (on 64bit arch)
+
| Int64 (on 64-bit arch)
 
| 2 words
 
| 2 words
 
|
 
|
 
|-
 
|-
| Int64 (on 32bit arch)
+
| Int64 (on 32-bit arch)
 
| 3 words
 
| 3 words
 
|
 
|
Line 61: Line 61:
 
| Word
 
| Word
 
| 2 words
 
| 2 words
| Due to alignment
+
|
 
|-
 
|-
 
| Word8
 
| Word8
Line 75: Line 75:
 
| Due to alignment
 
| Due to alignment
 
|-
 
|-
| Word64 (on 64bit arch)
+
| Word64 (on 64-bit arch)
 
| 2 words
 
| 2 words
  +
|
 
|-
 
|-
| Word64 (on 32bit arch)
+
| Word64 (on 32-bit arch)
 
| 3 words
 
| 3 words
 
|
 
|
 
|-
 
|-
| Double (on 64bit arch)
+
| Double (on 64-bit arch)
 
| 2 words
 
| 2 words
 
|
 
|
 
|-
 
|-
| Double (on 32bit arch)
+
| Double (on 32-bit arch)
 
| 3 words
 
| 3 words
 
|
 
|
Line 113: Line 114:
 
| [v]
 
| [v]
 
| (''1'' + 3N) words + N * sizeof(v)
 
| (''1'' + 3N) words + N * sizeof(v)
| single shared []
+
| Single shared []
 
|-
 
|-
 
| Data.ByteString
 
| Data.ByteString
Line 148: Line 149:
 
|-
 
|-
 
| Data.Vector v
 
| Data.Vector v
| (4 + (2+N)) words + N * sizeof(v)
+
| (7 + N) words + N * sizeof(v)
| ''O(1)'' slicing shares Array#
+
| Slices take 3N words + N * sizeof(v)
 
|}
 
|}

Latest revision as of 01:27, 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.

[edit] 1 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 32=bit architectures, and 8 bytes on 64-bit architectures. Sizes are usually rounded up to word-boundaries.

[edit] 1.1 Basic Types

Data type sizeof(T) Notes
() 0 words Single shared ()
Bool 0 words Single shared True/False
Char 2 words Char-sharing pool
Int 2 words
Int8 2 words Due to alignment
Int16 2 words Due to alignment
Int32 2 words Due to alignment
Int64 (on 64-bit arch) 2 words
Int64 (on 32-bit arch) 3 words
Word 2 words
Word8 2 words Due to alignment
Word16 2 words Due to alignment
Word32 2 words Due to alignment
Word64 (on 64-bit arch) 2 words
Word64 (on 32-bit arch) 3 words
Double (on 64-bit arch) 2 words
Double (on 32-bit arch) 3 words
Integer (small) 2 words
Integer (bignum rep.) 3 words + sizeof(bignum-repr) FIXME

[edit] 1.2 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 (7 + N) words + N * sizeof(v) Slices take 3N words + N * sizeof(v)