Memory Allocation Costs in Large C and C++ Programs

29
Memory Allocation Costs in Large C and C++ Programs An article by David Detlefs, Al Dosser and Benjamin Zorn Presented by Vered Ojalvo

description

Memory Allocation Costs in Large C and C++ Programs. An article by David Detlefs, Al Dosser and Benjamin Zorn Presented by Vered Ojalvo. Agenda. Introduction Programs Used in the Tests Allocators Used in the Tests Overview of a Memory Allocator Test Results Summary. Introduction. - PowerPoint PPT Presentation

Transcript of Memory Allocation Costs in Large C and C++ Programs

Page 1: Memory Allocation Costs in Large C and C++ Programs

Memory Allocation Costs in Large C and C++ Programs

An article by David Detlefs, Al Dosser and Benjamin Zorn

Presented by Vered Ojalvo

Page 2: Memory Allocation Costs in Large C and C++ Programs

Agenda

Introduction Programs Used in the Tests Allocators Used in the Tests Overview of a Memory Allocator Test Results Summary

Page 3: Memory Allocation Costs in Large C and C++ Programs

Introduction

Dynamic Storage Allocation Efficiency Importance

Many Algorithms for DSA Exist

Comparison Between Allocators is Needed

How to Compare Allocators?

Page 4: Memory Allocation Costs in Large C and C++ Programs

Programs

Drawn from Wide Variety of Application Areas

Most are Publicly Available

Used in Previous DSA Comparisons

Page 5: Memory Allocation Costs in Large C and C++ Programs

Programs (cont.)

Program Lang. Desc.

Sis C Circuit Synthesis

Geodesy C++ Debugger

Ild C++ Loader

Perl C Reporting Lang.

Xfig C Drawing Prog.

Page 6: Memory Allocation Costs in Large C and C++ Programs

Programs (cont.)

Program Lang. Desc.

Ghost C PS Interpreter

Make C GNU Make

Espresso C Logic Optim.

Ptc C Pascal to C

Gawk C GNU Awk

Cfrac C Factor Integers

Page 7: Memory Allocation Costs in Large C and C++ Programs

Allocation Information by Programs

ProgramLines of Source

Instr. Exec.(*10̂ 6)

Total Objects (*10̂ 3)

Total Bytes (*10̂ 3)

AverageSize(bytes)

Max.Objects(*10̂ 3)

Max.Bytes(*10̂ 3)

Alloc.Rate(Kb / Sec)

Sis 172000 64794.1 63395 15797173 249.2 48.5 1932.2 4120.8Geodesy 82500 2648.1 2517 42152 16.7 113.8 3880.6 324.3Ild 36000 381.3 33 24829 752.4 2.1 1278.7 220.3Perl 34500 1091 1604 34089 21.3 2.3 116.4 714.2Xfig 30500 52.4 25 1852 72.7 19.8 1129.3 372.1Ghost 29500 1196.5 924 89782 97.2 26.5 2129 1861.7Make 21000 53.7 23 539 23 10.4 208.1 282.5Espresso 15500 2400 1675 107062 63.9 4.4 280.1 1497.1Ptc 9500 353.9 103 2386 23.2 102.7 2385.8 202.4Gawk 8500 957.3 1704 67559 39.6 1.6 41 2050.1Cfrac 6000 202.5 522 8001 15.3 1.5 21.4 1145.9

Page 8: Memory Allocation Costs in Large C and C++ Programs

Allocators

Several Allocators Tested

Most are Public, One Commercially Available

Each Implements a Different Allocation Algorithm

Page 9: Memory Allocation Costs in Large C and C++ Programs

Allocators - List ofUltrix malloc

Implementation,Supplied withBerkley 4.2 andDEC Ultrix

BW 2.6+MS,IP ConservativeGarbage-Collector

GNU’ Variant HybridFirst-Fit /Segregated Alg.

Page 10: Memory Allocation Costs in Large C and C++ Programs

Allocators - List of (cont.)G++ First-Fit Roving Pointer

Alg. Using Bins ofDifferent Sizes

Qf Fast Segregated -Storage Algorithm (arrayof freelists)

Page 11: Memory Allocation Costs in Large C and C++ Programs

A Memory Allocator

Introduction

Goals

Algorithm

Implementation Considerations

Page 12: Memory Allocation Costs in Large C and C++ Programs

Memory Allocator - Introduction Written by Doug Lea

Publicly Available

Widely Used in Linux, embedded systems and PC environments

Page 13: Memory Allocation Costs in Large C and C++ Programs

Memory Allocator -Goals

Balance Between a Number of Goals:

Maximizing Compatibility

Maximizing Portability

Minimizing Space

Page 14: Memory Allocation Costs in Large C and C++ Programs

Memory Allocator -Goals (cont.) Minimizing Time

Maximizing Locality

Maximizing Error Detection

Page 15: Memory Allocation Costs in Large C and C++ Programs

Memory Allocator - Goal Discussion

Some say (Paul Wilson and others in

“Dynamic Storage Allocation: A

Survey and Critical Review”) that

the allocator’s primary goal is

minimizing space by minimizing

wastage.

Page 16: Memory Allocation Costs in Large C and C++ Programs

Memory Allocator - Goal Discussion (cont.)

Doug Lea believes that while

time/space issues dominate, the

set of tradeoffs and compromises

is nearly endless.

Page 17: Memory Allocation Costs in Large C and C++ Programs

Memory Allocator - Goal Discussion (cont.)For Example: Worst-case alignment increases

wastage by forcing skips of bytes to align chunks

Some methods that improve time/space for small programs may cause unacceptably worse time/space characteristics in large programs

Page 18: Memory Allocation Costs in Large C and C++ Programs

Memory Allocator - AlgorithmsTwo core elements:

Boundary Tags - Size Information Before and After the Chunk

Binning - Available Chunks are Maintained in Bins, and Grouped by Size

Page 19: Memory Allocation Costs in Large C and C++ Programs

Boundary Tags

Page 20: Memory Allocation Costs in Large C and C++ Programs

Binning

Page 21: Memory Allocation Costs in Large C and C++ Programs

Data Gathering Methods

Instruction Counts Using Larus QPT’ tool Provides Per-Procedure Counts

Execution time Measured Using the c-shell time Command

Program Live Data Using a Modified malloc / free

Page 22: Memory Allocation Costs in Large C and C++ Programs

Tests Results

How Many Instructions (Absolute and Relative) for Every malloc / free Operation

Using All Combinations of Programs and Allocators

Page 23: Memory Allocation Costs in Large C and C++ Programs

Test ResultsProgram

Ultrix (Instr/malloc)

BW 2.6+ms,ip (Instr/malloc)

GNU' (Instr/malloc)

G++ (Instr/malloc)

Qf (Instr/malloc)

absol relat absol relat absol relat absol relat absol relatSis 60 1 489 8.15 101 1.68 84 1.4 192 3.2Geodesy 46 1 179 3.89 81 1.76 53 1.15 29 0.63Ild 57 1 3544 62.18 92 1.61 99 1.74 76 1.33Perl 46 1 207 4.5 82 1.78 44 0.96 37 0.8Xfig 59 1 543 9.2 105 1.78 62 1.05 89 1.51Ghost 56 1 614 10.96 101 1.8 71 1.27 74 1.32Make 48 1 296 6.17 91 1.9 74 1.54 43 0.9Espresso 50 1 208 4.16 90 1.8 77 1.54 40 0.8Ptc 55 1 456 8.29 101 1.84 66 1.2 47 0.85Gawk 49 1 83 1.69 84 1.71 54 1.1 39 0.8Cfrac 47 1 106 2.26 86 1.83 30 0.64 33 0.7

Average 52 1 611 11.04 92 1.77 65 1.24 64 1.17

Absolute and Relative Instructions per Call to Malloc. Relat is relative to Ultrix = 1.

Page 24: Memory Allocation Costs in Large C and C++ Programs

Test ResultsProgram

Ultrix (Instr/free)

BW 2.6+ms,ip (Instr/free)

GNU' (Instr/free)

G++ (Instr/free)

Qf (Instr/free)

absol relat absol relat absol relat absol relat absol relatSis 18 1 2 0.11 71 3.94 8 0.44 53 2.94Geodesy 18 1 2 0.11 66 3.67 8 0.44 25 1.39Ild 18 1 2 0.11 74 4.11 8 0.44 42 2.33Perl 18 1 2 0.11 82 4.56 8 0.44 31 1.72Xfig 18 1 2 0.11 71 3.94 8 0.44 39 2.17Ghost 18 1 2 0.11 88 4.89 8 0.44 56 3.11Make 18 1 2 0.11 83 4.61 8 0.44 28 1.56Espresso 18 1 2 0.11 84 4.67 8 0.44 32 1.78Ptc 18 1 2 0.11 113 6.28 8 0.44 90 5Gawk 18 1 2 0.11 81 4.5 8 0.44 32 1.78Cfrac 18 1 2 0.11 83 4.61 8 0.44 26 1.44

Average 18 1 2 0.11 81 4.53 8 0.44 41 2.29

Absolute and Relative Instructions per Call to Free. Relat is relative to Ultrix = 1.

Page 25: Memory Allocation Costs in Large C and C++ Programs

Test Results

Number of Instructions per Object Allocated (absolute and relative)

Number of Instructions in malloc, free and realloc (and all related routines), by Total Number of Instructions

Page 26: Memory Allocation Costs in Large C and C++ Programs

Test Results

Program

Ultrix (% Exec.

Time)

BW 2.6+ms,ip (% Exec. Time)

GNU' (% Exec.

Time)

G++ (% Exec.

Time)

Qf (% Exec.

Time)absol relat absol relat absol relat absol relat absol relat

Sis 8.2 1 69.3 8.45 18.2 2.22 9.8 1.2 26 3.17Geodesy 6.4 1 19.6 3.06 14.5 2.27 6.1 0.95 5.3 0.83Ild 0.6 1 37.1 61.83 1.6 2.67 0.9 1.5 0.9 1.5Perl 9.4 1 33.7 3.59 26.1 2.78 7.5 0.8 10.8 1.15Xfig 3.2 1 34.5 10.78 6.6 2.06 3.5 1.09 4.9 1.53Ghost 6 1 54.8 9.13 15.3 2.55 6.5 1.08 10.5 1.75Make 2.6 1 14.3 5.5 6.2 2.38 3.5 1.35 2.6 1Espresso 5 1 23.2 4.64 12.8 2.56 6.2 1.24 5.3 1.06Ptc 1.6 1 14.3 8.94 3 1.88 1.9 1.19 1.4 0.87Gawk 13.6 1 65.5 4.82 33.6 2.47 11.9 0.88 14.5 1.07Cfrac 20.1 1 34.1 1.7 52.3 2.6 11.7 0.58 18.3 0.91

Average 6.97 1 36.4 11.13 17.29 2.4 6.32 1.08 9.14 1.35

Percent Storage Allocation Instr/Application Instr. Relat is relative to Ultrix = 1.

Page 27: Memory Allocation Costs in Large C and C++ Programs

Test Results

ProgramUltrix Frag.

BW 2.6+ms,ip (% Frag.

GNU' Frag.

G++ Frag.

Qf Frag.

absol relat absol relat absol relat absol relat absol relatSis 1.79 1 3.99 2.22 1.25 0.7 1.47 0.82 4.56 2.54Geodesy 1.71 1 1.94 1.13 1.21 0.71 1.3 0.76 1.28 0.75Ild 1.44 1 2.86 1.98 1.11 0.77 1.22 0.84 1.17 0.81Perl 1.99 1 5.42 2.73 1.42 0.72 1.27 0.64 1.27 0.64Xfig 1.62 1 2.21 1.37 1.43 0.89 1.41 0.87 1.43 0.88Ghost 1.7 1 2.53 1.49 1.36 0.8 1.27 0.74 1.16 0.68Make 1.92 1 2.87 1.5 1.51 0.78 1.61 0.84 1.65 0.86Espresso 2.9 1 4.34 1.5 1.24 0.43 1.17 0.4 1.49 0.52Ptc 1.48 1 1.48 1 1.47 0.99 1.44 0.98 1.35 0.91Gawk 1.98 1 8.8 4.43 2.08 1.05 1.6 0.81 1.6 0.81Cfrac 3.06 1 24.09 7.87 3.06 1 2.29 0.75 3.06 1

Average 1.96 1 5.5 2.48 1.56 0.8 1.46 0.77 1.82 0.95

Heap Expansion due to Fragmentation. Relat is relative to Ultrix = 1.

Page 28: Memory Allocation Costs in Large C and C++ Programs

Summary

The Paper Presents Detailed

Measurements of the Costs of

Dynamic Storage Allocation in 11

Diverse C and C++ Programs

Using 5 Different DSA

Implementations

Page 29: Memory Allocation Costs in Large C and C++ Programs

Thank You