Emacs Garbage collection improvements by using the GCMH package

(Date: 1 October 2025)

Summary

One way to configure Garbage Collection is through the ELPA GCMH package.

Introduction

In a previous article on IGC, I covered how the IGC branch of Emacs can lead to more effective Garbage Collection (GC). This depends on the MPS library, included in Emacs. However, whilst MPS can be built on GNU/Linux x86-64, and other major platforms, it doesn’t build on ARM platforms running GNU/Linux, at the time of writing. For example, MPS is not available on Raspberry Pi, so IGC won’t be available on it either.

For this of course, we can still use the traditional GC included in Emacs.

There is a straightforward of implementing a GC strategy by using the GCMH package.

Overview of using GCMH

GCMH is available on ELPA, which you can install with M-x package-install GCMH. This was written by Andrea Corallo, co-maintainer of Emacs.

The GCMH Package documentation

1 GCMH - the Garbage Collector Magic Hack
═════════════════════════════════════════

  Enforce a sneaky Garbage Collection strategy to minimize GC
  interference with user activity.

  During normal use a high GC threshold is set.

  When idling GC is triggered and a low threshold is set.

  A more detailed explanation of the rationale behind this can be found
  at:

  <http://akrl.sdf.org/>

...

(gcmh-mode 1)

For more detail, tuning the GC can be subtle depending on your needs. Read the manual with M-: (info "(elisp) Garbage Collection") RET, or at Garbage Collection)

where you can read about a variable such as the following:

gc-cons-threshold is a variable defined in ‘src/alloc.c’.

Its value is 1000000
Original value was 800000

Number of bytes of consing between garbage collections.
Garbage collection can happen automatically once this many bytes have been
allocated since the last garbage collection.  All data types count.

Garbage collection happens automatically only when ‘eval’ is called.

By binding this temporarily to a large number, you can effectively
prevent garbage collection during a part of the program.  But be
sure to get back to the normal value soon enough, to avoid system-wide
memory pressure, and never use a too-high value for prolonged periods
of time.

Conclusion

Garbage collection is an important part of the effectiveness of Lisp and Emacs. There are several ways to improve the responsiveness of Emacs. You can experiment with GC settings, and try the GCMH package.