Effiziente Programmierung in c Dynamic Memory Allocation A - - PowerPoint PPT Presentation

effiziente programmierung in c dynamic memory allocation
SMART_READER_LITE
LIVE PREVIEW

Effiziente Programmierung in c Dynamic Memory Allocation A - - PowerPoint PPT Presentation

Effiziente Programmierung in c Dynamic Memory Allocation A presentation from : Marcel Ellermann Seminar Effizientes Programmieren in C Dynamic Memory Allocation Marcel Ellermann 1 Agenda Introduction to Memory Management


slide-1
SLIDE 1

Effiziente Programmierung in c Dynamic Memory Allocation

A presentation from : Marcel Ellermann

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

1

slide-2
SLIDE 2

Agenda

  • Introduction to Memory Management
  • Basics of dynamic memory allocation
  • Introduction to First-Fit, Next-Fit and Best-Fit
  • Runtime comparison
  • Summary

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

2

slide-3
SLIDE 3

Introduction to Memory Management

  • Physical memory is described with pages
  • Page size is architecture dependent
  • X86 Architecture has 4KB Pages
  • Often more than one page size is supported
  • ARM supports 4KB, 8KB and 32KB Pages*

*http://www.makelinux.net/books/lkd2/ch19lev1sec6 Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

3

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-4
SLIDE 4

Introduction to Memory Management

  • Kernel has its own address space
  • Each process has its own address space
  • Processes run in a virtual address space
  • Virtual addresses are mapped to physical addresses by pages

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

4

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-5
SLIDE 5

Introduction to Memory Management

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

5

From http://www.ibm.com/developerworks/linux/library/l-kernel-memory-access/index.html Accessed on 09th January 2014

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-6
SLIDE 6

Introduction to Memory Management

  • The full virtual address space is not mapped to physical

memory right from the start

  • On a system with 4KB Pages and 2GB Memory and a page

size of 40 Byte the paging table would consume about 20MB.

  • The process has to request mapping from its virtual address

space to physical memory.

  • Linux offers the brk/sbrk and mmap interface.

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

6

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-7
SLIDE 7

Introduction to Memory Management

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

7

  • Each thread receives a default mapping of 8

MB at its highest available memory adress

  • Space is the programm stack
  • Stack pointer to the „top“ of the stack
  • Grows from highest adress to lowest
  • Only elements at the „top“ can be removed

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-8
SLIDE 8

Basics of dynamic memory allocation

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

8

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-9
SLIDE 9

Basics of dynamic memory allocation

  • If we free an allocated block we create wholes
  • f free memory
  • We do not want to waste that free memory
  • Need to track free memory
  • Need to handle our own memory space – can

not use the heap

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

9

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-10
SLIDE 10

Basics of dynamic memory allocation

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

10 10

Source: The C programming Language; W. Kerningham & M. Ritchie; Pentice-Hall 1988

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-11
SLIDE 11

Basics of dynamic memory allocation

Requesting memory

  • Usually the requested size is given in bytes
  • Allocator has to select a free memory block from his free list
  • Various approaches how to select the free memory block
  • Allocator guarantees to return at least the requested block

size

  • Usually NULL is returned if no free memory is available

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

11 11

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-12
SLIDE 12

Basics of dynamic memory allocation

Requesting memory from OS

  • Processes have nearly no virtual memory mapped by default
  • Program

ram break ak = the end of the process‘s data segment

  • Increase that end with a call to the systems sbrk method
  • System calls are slow
  • Usually larger memory amounts are requested and than

distributed to the process upon request.

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

12 12

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-13
SLIDE 13

Basics of dynamic memory allocation

Freeing memory

  • The memory chunk has to be returned into the free list
  • Free list is ordered by ascending memory address
  • Memory block is inserted at its respective place within the
  • rder

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

13 13

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-14
SLIDE 14

Basics of dynamic memory allocation

  • External

nal fragm gmentation: entation: A form of fragmentation that arises when memory is allocated in units of arbitrary size. When a large amount of memory is released, part of it may be used to meet a subsequent request, leaving an unused part that is too small to meet any further requests.*

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

14 14 *JOHN DAINTITH. "external fragmentation." A Dictionary of Computing. 2004.Encyclopedia.com. (January 15, 2014).

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-15
SLIDE 15

Basics of dynamic memory allocation

Coalescing

  • T
  • reduce external fragmentation blocks returned to the free

list are coalesced with each other

  • If block start at the end of a free block they’re merged
  • If block ends at the start of a free block they’re merged

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

15 15

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-16
SLIDE 16

Basics of dynamic memory allocation

Miscellaneous

  • Though the allocator has freed its memory, it‘s not

guaranteed to be returned to the operating system

  • OS can only reserve full memory pages for a process

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

16 16

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-17
SLIDE 17

Basics of dynamic memory allocation

Header structure

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

17 17

typedef long Align; union Header { struct { union Header *ptr; unsigned size; } s; Align x; /*Never used but forces alignment*/ };

  • Header also used to align data
  • Only multiples of the headers‘ size are allocated

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-18
SLIDE 18

Dynamic memory allocation methods

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

18 18

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-19
SLIDE 19

Dynamic memory allocation methods

First-Fit

  • First fit searches the Free-List from the beginning
  • Returns the first chunk big enough to hold at least the

requested amount of bytes

  • If the chunk is too big it‘s split and the remaining space is left

in the Free-List.

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

19 19

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-20
SLIDE 20

Dynamic memory allocation methods

Next-Fit

  • Similar to First-Fit
  • Start searching from the block that pointed to the last

allocated one

  • Should drastically increase the performance
  • Average lookups should decrease
  • First-Fit tends to accumulate small block at its beginning but Next-Fit

distributes the blocks uniformly.

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

20 20

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-21
SLIDE 21

Dynamic memory allocation methods

Best-Fit

  • Uses the free block that fits the best to the requested size.
  • Has to search the whole list for a best match
  • T

ends to leave small unusable blocks

  • Therefore produces external fragmentation

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

21 21

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-22
SLIDE 22

Dynamic memory allocation methods

Optimization

  • T
  • reduce external fragmentation usually a minimal block size

is defined

  • If requested < available and requested + min > available the

whole block is returned and not split.

  • Store available chunks in bins grouped by size

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

22 22

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-23
SLIDE 23

Dynamic memory allocation methods

Bins

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

23 23

http://gee.cs.oswego.edu/dl/html/malloc.html (15.01.2014)

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-24
SLIDE 24

Dynamic memory allocation methods

Bins

  • 128 fixed-width bins
  • Bins up to 512 bytes are each just spaced 8 bytes apart
  • Only hold the exact size they’re representing
  • Bins over the 512 byte mark hold a range of chunks

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

24 24

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-25
SLIDE 25

Dynamic memory allocation methods

Bins

  • Till 1995 the oldest-first rule has been applied
  • Nowadays chunks are sorted within bins
  • It is has been proven that “the minor time investment was

worth it to avoid observed bad cases”*.

  • Best-Fit with the presented optimizations even reduces

external fragmentation compared to other methods

*http://gee.cs.oswego.edu/dl/html/malloc.html Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

25 25

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-26
SLIDE 26

Dynamic memory allocation methods

Caching

  • Caching refers to the fact that small blocks are not coalesced

immediately

  • Each split and each coalesce requires time
  • Small blocks are kept up to a given number
  • Very effective for many small allocations and frees i.e. when

working with trees

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

26 26

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-27
SLIDE 27

Dynamic memory allocation methods

glib - malloc

“This is not the fastest, most space-conserving, most

portable, or most tunable malloc ever written. However it is among the fastest while also being among the most space-conserving, portable and

  • tunable. Consistent balance across these factors results

in a good general-purpose allocator for malloc-intensive programs.”*

*http://code.woboq.org/userspace/glibc/malloc/malloc.c.html Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

27 27

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-28
SLIDE 28

Dynamic memory allocation methods

glib - malloc

  • For Blocks with a size <= 64 bytes the algorithm uses a

caching allocator

  • Block with a size >= 512 bytes the algorithm uses a pure

best-fit allocator

  • In between “it does the best it can trying to meet both goals

at once”*

*http://code.woboq.org/userspace/glibc/malloc/malloc.c.html

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

28 28

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-29
SLIDE 29

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

29 29

Runtime comparision

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-30
SLIDE 30

Runtime comparison

  • We perform INS_MAX cycles
  • Each cycles between 1 and 10 allocations are performed
  • Each allocation allocates between 1 and 8 kb
  • Each allocation lasts between 1 clock and 10 seconds
  • All random values are distributed uniformly
  • Comparison done for First-Fit, Next-Fit, glib’s malloc

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

30 30

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-31
SLIDE 31

Runtime comparison

  • For 100.000 cycles
  • First

st-Fit: Fit: 157 seco cond nds

  • Next-Fit:

Fit: 168 seco cond nds

  • glib’s malloc

loc: 78 secon

  • nds

ds

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

31 31

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-32
SLIDE 32

Runtime comparison

Evaluation

  • Malloc outperforms each of the custom implementations
  • Despite the fact that next-fit should be a huge improvement

it’s even slower

  • Differences might be to flaws in own implementations and

some missing or ineffective optimizations

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

32 32

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-33
SLIDE 33

Summary

  • Virtual memory
  • Sbrk, mmap
  • External fragmentation
  • Best-Fit, Next-Fit, First-Fit
  • Free-List
  • Binning
  • Caching

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

33 33

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son

slide-34
SLIDE 34

Literature

  • Donald E. Knuth, The art of computer programming -

Fundamental algorithms, 2006, Addison Wesley

  • Carter Bays, A Comparison of Next-fit, First-fit and Best-fit,

1977, Volume 20, Number 3, Communications of the ACM,

  • pp. 191—192
  • John E. Shore, On the external storage fragmentation

produced by first-fit allocation strategies, 1975, Volume 18, Number ?, Communications of the ACM, pp. 433—440

  • Robert Love, Linux Kernel Development, Novell Press, 2005,

Edition 2

  • Brian W. Kernighan and Dennis M. Ritchie, The C

programming Language, Prentice-Hall, 1988

Seminar – Effizientes Programmieren in C – Dynamic Memory Allocation – Marcel Ellermann

34 34

Introdu

  • duction
  • n

Basi sics s of dynamic mic memory manageme ment Alloc

  • cation
  • n methods

ds Compari rison son