Book Image

D Cookbook

By : Adam Ruppe
Book Image

D Cookbook

By: Adam Ruppe

Overview of this book

Table of Contents (21 chapters)
D Cookbook
Credits
Foreword
About the Author
About the Reviewers
www.PacktPub.com
Preface
Index

Avoiding the garbage collector


D programs typically use a garbage collector that can make memory management both easier and more efficient. With the garbage collector, you don't have to keep track of object ownership and can defer free memory to a later point, which can boost performance.

However, while the garbage collector is useful, it doesn't absolve you of all thought in the area of memory management, especially when performance is important. In tight loops, you'll want to avoid the garbage collector. The way to do this is to avoid garbage collection allocations. No garbage collection allocation means no garbage collection cycle.

How to do it…

In order to avoid the garbage collector, perform the following steps:

  1. Find the hotspots in your application. Typically, this means focusing on your innermost loops.

  2. Remove functions and operations that call into the GC:

    • Array literals, except the ones initializing a variable marked static

    • Array append or concatenation

    • Array length assignment

    • Any associative...