On this page:

16.4 Garbage Collection

Set the PLTDISABLEGC environment variable (to any value) before Racket starts to disable garbage collection. Set the PLT_INCREMENTAL_GC environment variable to a value that starts with 1, y, or Y to request incremental mode at all times, but calling (collect-garbage 'incremental) in a program with a periodic task is generally a better mechanism for requesting incremental mode.

In Racket 3m (the main variant of Racket), each garbage collection logs a message (see Logging) at the 'debug level with topic 'GC. The data portion of the message is an instance of a gc-info prefab structure type with 10 fields as follows, but future versions of Racket may use a gc-info prefab structure with additional fields:

(struct gc-info (mode pre-amount pre-admin-amount code-amount
                      post-amount post-admin-amount
                      start-process-time end-process-time
                      start-time end-time)

The format of the logged message’s text is subject to change. Currently, after a prefix that indicates the place and collection mode, the text has the format

used(admin)[code]; free reclaimed(adjust) elapsed @ timestamp



Collectable memory in use just prior to garbage collection



Additional memory used as to manage collectable memory



Additional memory used for generated machine code



Collectable memory reclaimed by garbage collection



Negation of change to administrative memory minus reclaimed



Processor time used to perform garbage collection



Processor time since startup of garbage collection’s start

Changed in version of package base: Added PLT_INCREMENTAL_GC.


(collect-garbage [request])  void?

  request : (or/c 'major 'minor 'incremental) = 'major
Requests an immediate garbage collection or requests a garbage-collection mode, depending on request:

Changed in version 6.3 of package base: Added the request argument. Changed in version Added 'incremental mode.


(current-memory-use [cust])  exact-nonnegative-integer?

  cust : custodian? = #f
Returns an estimate of the number of bytes of memory occupied by reachable data from cust. This estimate is calculated by the last garbage collection, and can be 0 if none occurred (or if none occurred since the given custodian was created). The current-memory-use function does not perform a collection by itself; doing one before the call will generally decrease the result (or increase it from 0 if no collections happened yet).

If cust is not provided, the estimate is a total reachable from any custodians.

When Racket is compiled without support for memory accounting, the estimate is the same (i.e., all memory) for any individual custodian; see also custodian-memory-accounting-available?.

See also vector-set-performance-stats!.


(dump-memory-stats v ...)  any

  v : any/c
Dumps information about memory usage to the low-level error port or console.

Various combinations of v arguments can control the information in a dump. The information that is available depends on your Racket build; check the end of a dump from a particular build to see if it offers additional information; otherwise, all vs are ignored.