► |  | The Racket Reference

1 | Language Model
2 | Syntactic Forms
3 | Datatypes
4 | Structures
5 | Classes and Objects
6 | Units
7 | Contracts
8 | Pattern Matching
9 | Control Flow
10 | Concurrency and Parallelism
11 | Macros
12 | Input and Output
13 | Reflection and Security
14 | Operating System
15 | Memory Management
16 | Unsafe Operations
17 | Running Racket
| Bibliography
| Index

▼ | 15 | Memory Management

15.1 | Weak Boxes
15.2 | Ephemerons
15.3 | Wills and Executors
15.4 | Garbage Collection

On this page:

collect-garbage
current-memory-use
dump-memory-stats

top← prev up next →

#### 15.4`` Garbage Collection

Set the PLTDISABLEGC environment variable (to any value) before Racket starts to disable garbage collection.

In Racket 3m (the main variant of Racket), each garbage collection logs a message (see Logging) at the 'debug level. 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 (major? pre-amount pre-admin-amount code-amount
> post-amount post-admin-amount
> start-process-time end-process-time
> start-time end-time)
> #:prefab)

The major? field indicates whether the collection was a “major” collection that inspects all memory or a “minor” collection that mostly inspects just recent allocations. The pre-amount field reports place-local memory use (i.e., not counting the memory use of child places) in bytes at the time that the garbage collection started. The pre-admin-amount is a larger number that includes memory use for the garbage collector’s overhead (such as space on memory pages that is not yet used). The code-amount field reports additional memory use for generated native code (which is the same just before and after a garbage collection, since it is released via finalization). The post-amount and post-admin-amount fields correspond to pre-amount and pre-admin-amount, but after garbage collection. The start-process-time and end-process-time fields report processor time (in the sense of current-process-milliseconds) at the start and end of garbage collection; the difference is the processor time consumed by collection. The start-time and end-time fields report real time (in the sense of current-inexact-milliseconds) at the start and end of garbage collection; the difference is the real time consumed by garbage collection.

> > procedure

> > (collect-garbage) → void?

Forces an immediate garbage collection (unless garbage collection is disabled by setting PLTDISABLEGC). Some effectively unreachable data may remain uncollected, because the collector cannot prove that it is unreachable.

The collect-garbage procedure provides some control over the timing of collections, but garbage will obviously be collected even if this procedure is never called (unless garbage collection is disabled).

> > procedure

> > (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?.

> > procedure

> > (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.

top← prev up next →