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 array operation or literal
    • The new expression
    • Closures, except the ones marked scope in the function's parameter list
    • Nested structs that use local variables that are returned from a function
    • Any function that does one of these things
  3. Move the allocations outside the hotspot or replace them with alternatives:
    • Make static and immutable array literal initializers
    • Use static array buffers when possible instead of dynamically allocated arrays
    • Use stack-allocated classes
    • Mark delegate arguments scope in the function definition

      Tip

      If you load your program in a debugger and set a break point at the beginning of your hotspot, you can then break on the gc_alloc and gc_qalloc functions to find allocations at runtime. Also, if you use the ldc compiler, try the –nogc command-line switch when compiling a module.

How it works…

The D garbage collector only runs at program termination and when a garbage collector allocation is attempted. It doesn't have enough available memory to fulfil the request. Garbage collector allocations are not necessarily easy to find. The syntax may not stand out or they may be hidden behind library functions; however, if you successfully avoid these language constructs, you will successfully avoid the garbage collector.

You'll still have to manage your resources. We'll look at various techniques to help with this throughout the rest of this chapter.

Note

Can we write a program without a garbage collector at all? Yes, in fact, we can write D programs that use the majority of the language, yet don't use the full D runtime or standard library at all and will even work on bare metal without an operating system. However, since this severely limits your library choices, it isn't always a practical option.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.136.233.153