5 Storage Management in Common Lisp
change-memory-management &key :growth-limit :growth-rate :expand :expand-p :reclamation-ratio :reserved-growth-rate :expand-reserved :reserved-dynamic :help
change-memory-management
alters the parameters that affect storage allocation.
:growth-limit
:growth-rate
:expand
:growth-limit
keyword argument.
:expand-p
t
for:expand-p
specifies that rather than performing a garbage collection, memory should be expanded at the next point at which additional storage is required.
:reclamation-ratio
:reserved-growth-rate
:expand-reserved
:reserved-dynamic
*gc-silence*
. The default value is 1024 bytes.
:help
> (room t) ;;; 1030 words [4120 bytes] of dynamic storage in use. ;;; 457464 words [1829856 bytes] of free storage available ;;; before a GC. 915958 words [3663832 bytes] of free ;;; storage available if GC is disabled. ;;; Semi-space Size: 1792K bytes [28 segments] ;;; Current Dynamic Area: Dynamic-0-Area ;;; GC Status: Enabled ;;; Reserved Free Space: 3776K bytes [59 segments] ;;; Memory Growth Limit: 32768K bytes [512 segments], total ;;; Memory Growth Rate: 256K bytes [4 segments] ;;; Reclamation Ratio: 33% desired free after garbage collection ;;; Area Information: ;;; Name Size [used/allocated] ;;; ---- ---- ;;; Ephemeral Level 0 9K/512K bytes, 1/8 segments ;;; Ephemeral Level 1 0K/640K bytes, 0/10 segments ;;; Ephemeral Level 2 0K/640K bytes, 0/10 segments ;;; Ephemeral Odd-level OSP 0K/512K bytes, 0/8 segments ;;; Ephemeral Even-level OSP 0K/640K bytes, 0/10 segments ;;; Dynamic-0-Area 5K/1791K bytes, 1/28 segments ;;; Dynamic-1-Area 0K/1792K bytes, 0/28 segments ;;; Discardable-Static-Area 2K/64K bytes, 1/1 segment ;;; Discardable-Read-Write-Area 588K/640K bytes, 10/10 segments ;;; Foreign Area 37K/64K bytes, 1/1 segment ;;; Static-Area 523K/576K bytes, 9/9 segments ;;; Read-Write-Area 703K/704K bytes, 11/11 segments ;;; Readonly-Pointer-Area 1144K/1152K bytes, 18/18 segments ;;; Readonly-Non-Pointer-Area 3770K/3776K bytes, 59/59 segments NILdynamic-gc Function> (change-memory-management :growth-limit 300 :growth-rate 16 :reclamation-ratio 0.25) T
> (room t) ;;; 1106 words [4424 bytes] of dynamic storage in use. ;;; 457388 words [1829552 bytes] of free storage available ;;; before a GC. 915882 words [3663528 bytes] of free ;;; storage available if GC is disabled. ;;; Semi-space Size: 1792K bytes [28 segments] ;;; Current Dynamic Area: Dynamic-0-Area ;;; GC Status: Enabled ;;; Reserved Free Space: 3776K bytes [59 segments] ;;; Memory Growth Limit: 19200K bytes [300 segments], total ;;; Memory Growth Rate: 1024K bytes [16 segments] ;;; Reclamation Ratio: 25% desired free after garbage collection ;;; Area Information: ;;; Name Size [used/allocated] ;;; ---- ---- ;;; Ephemeral Level 0 10K/512K bytes, 1/8 segments ;;; Ephemeral Level 1 0K/640K bytes, 0/10 segments ;;; Ephemeral Level 2 0K/640K bytes, 0/10 segments ;;; Ephemeral Odd-level OSP 0K/512K bytes, 0/8 segments ;;; Ephemeral Even-level OSP 0K/640K bytes, 0/10 segments ;;; Dynamic-0-Area 5K/1791K bytes, 1/28 segments ;;; Dynamic-1-Area 0K/1792K bytes, 0/28 segments ;;; Discardable-Static-Area 2K/64K bytes, 1/1 segment ;;; Discardable-Read-Write-Area 588K/640K bytes, 10/10 segments ;;; Foreign Area 37K/64K bytes, 1/1 segment ;;; Static-Area 523K/576K bytes, 9/9 segments ;;; Read-Write-Area 703K/704K bytes, 11/11 segments ;;; Readonly-Pointer-Area 1144K/1152K bytes, 18/18 segments ;;; Readonly-Non-Pointer-Area 3770K/3776K bytes, 59/59 segments NIL
dynamic-gc
dynamic-gc
replaces the old functiongc
. It invokes the Dynamic Garbage Collector.
gc
message when the variable*gc-silence*
is set tonil
, which is the default.
> (room) ;;; 684 words [2736 bytes] of dynamic storage in use. ;;; 523346 words [2093384 bytes] of free storage available before a GC. ;;; 1047376 words [4189504 bytes] of free storage available if GC is disabled. T> (dynamic-gc) ;;; GC: 612 words [2448 bytes] of dynamic storage in use. ;;; 523674 words [2094696 bytes] of free storage available before a GC. ;;; 1047960 words [4191840 bytes] of free storage available if GC is disabled. 2576 2093544 4189664
> (room) ;;; 644 words [2576 bytes] of dynamic storage in use. ;;; 523386 words [2093544 bytes] of free storage available before a GC. ;;; 1047416 words [4189664 bytes] of free storage available if GC is disabled. T
gc
egc-off
egc-off
disables ephemeral garbage collection.
egc-on
egc-on
egc-on
enables ephemeral garbage collection.
compile
,compile-file
, orcompiler-options
with the value of the:egc
keyword argument specified ast
. If you attempt to load incompatible code while the Ephemeral Garbage Collector is running or if you enable the Ephemeral Garbage Collector while you are running incompatible code, a continuable error is signaled.
compile
, compile-file
, compiler-options
, egc-off
,egc-options
egc-options &key :level-sizes :ephemeral-size-limit
egc-options
determines the configuration of the ephemeral levels.
:level-sizes
keyword argument determines the number and size of the ephemeral storage levels. The value of the argument is a list of numbers; the length of the list determines the number of ephemeral levels, and each element of the list determines the size, in segments, of the corresponding level. By default, there are three ephemeral levels: level 0 is 8 segments large, level 1 is 10 segments, and level 2 is 10 segments.
:ephemeral-size-limit
keyword argument determines the size, in fixnum words, of the largest object that can be allocated in an ephemeral level. Any object that is larger than this limit is allocated in the current dynamic area. The value of this argument must be less than 8192 words; the default value is 4096 words. A change to the argument value takes effect immediately.
change-memory-management
. Then callegc-options
again to specify the number of ephemeral levels.
egc-options
is called with no arguments, it returns a list of the current parameter settings::level-sizes
gives the value of the:level-sizes
argument.:actual-sizes
gives the actual sizes of the ephemeral levels.:ephemeral-size-limit
gives the value of the:ephemeral-size-limit
argument.> (egc-options) ((:LEVEL-SIZES 8 10 10) (:ACTUAL-SIZES 8 10 10) (:EPHEMERAL-SIZE-LIMIT . 4096));;; Increase the size of ephemeral space to 40 segments. > (egc-options :level-sizes '(10 10 10 10)) ;;; Disabling ephemeral garbage collection. ;;; Enabling ephemeral garbage collection. NIL
;;; This example assumes that the semi-space size is 32 segments. ;;; Therefore, the requested increase exceeds the size of one ;;; dynamic semi-space, and Lisp allocates only 32 segments. > (egc-options) ((:LEVEL-SIZES 10 10 10 10) (:ACTUAL-SIZES 8 8 8 8) (:EPHEMERAL-SIZE-LIMIT . 4096))
;;; Expand dynamic memory by 8 segments. > (change-memory-management :expand 8) ;;; Expanding Dynamic Memory T
;;; Note that expanding dynamic memory does not affect ;;; the size of ephemeral space. > (egc-options) ((:LEVEL-SIZES 10 10 10 10) (:ACTUAL-SIZES 8 8 8 8) (:EPHEMERAL-SIZE-LIMIT . 4096))
;;; Specify the desired ephemeral configuration again. > (egc-options :level-sizes '(10 10 10 10)) ;;; Disabling ephemeral garbage collection. ;;; Enabling ephemeral garbage collection. NIL
> (egc-options) ((:LEVEL-SIZES 10 10 10 10) (:ACTUAL-SIZES 10 10 10 10) (:EPHEMERAL-SIZE-LIMIT . 4096))
egc-state
egc-state
egc-state
returns information about the current state of the ephemeral levels.
(used-words . available-words)
. The function returnsnil
if ephemeral garbage collection is disabled.
> (egc-state) ((192 . 524088) (0 . 655352) (0 . 655352))ephemeral-gc Function> (ephemeral-gc) NIL
> (egc-state) ((72 . 524208) (144 . 655208) (0 . 655352))
ephemeral-gc
&optional force-to-dynamic
ephemeral-gc
explicitly empties the ephemeral levels by calling the Ephemeral Garbage Collector. You can use this function to explicitly delay the next garbage collection.
t
, all of the ephemeral levels are emptied into dynamic space.
gc
gc
gc
invokes both the Dynamic Garbage Collector and the Ephemeral Garbage Collector.
gc
message when the variable*gc-silence*
is set tonil
, which is the default.
> (room) ;;; 792 words [3168 bytes] of dynamic storage in use. ;;; 523238 words [2092952 bytes] of free storage available before a GC. ;;; 1047268 words [4189072 bytes] of free storage available if GC is disabled. T> (gc) ;;; GC: 612 words [2448 bytes] of dynamic storage in use. ;;; 523674 words [2094696 bytes] of free storage available before a GC. ;;; 1047960 words [4191840 bytes] of free storage available if GC is disabled. 2576 2093544 4189664
> (room) ;;; 644 words [2576 bytes] of dynamic storage in use. ;;; 523386 words [2093544 bytes] of free storage available before a GC. ;;; 1047416 words [4189664 bytes] of free storage available if GC is disabled. T
ephemeral-gc
, dynamic-gc
, *gc-silence*
gc-off
&optional no-reconsideration
gc-off
disables dynamic garbage collection. If ephemeral garbage collection is on, it is also disabled.
t
, you can use all the available storage without being asked to reconsider. The default value isnil
.
Dynamic-1-Area
is the current dynamic area, a dynamic garbage collection occurs whengc-off
is called.
> (room t) ;;; 1404 words [5616 bytes] of dynamic storage in use. ;;; 784770 words [3139080 bytes] of free storage available ;;; before a GC. 1570944 words [6283776 bytes] of free ;;; storage available if GC is disabled. ;;; Semi-space Size: 3072K bytes [48 segments] ;;; Current Dynamic Area: Dynamic-1-Area ;;; GC Status: Enabled ;;; Reserved Free Space: 3712K bytes [58 segments] ;;; Memory Growth Limit: 32768K bytes [512 segments], total ;;; Memory Growth Rate: 256K bytes [4 segments] ;;; Reclamation Ratio: 33% desired free after garbage collection ;;; Area Information: ;;; Name Size [used/allocated] ;;; ---- ---- ;;; Ephemeral Level 0 1K/512K bytes, 1/8 segments ;;; Ephemeral Level 1 1K/640K bytes, 1/10 segments ;;; Ephemeral Level 2 0K/640K bytes, 0/10 segments ;;; Ephemeral Odd-level OSP 0K/512K bytes, 0/8 segments ;;; Ephemeral Even-level OSP 0K/640K bytes, 0/10 segments ;;; Dynamic-0-Area 0K/3072K bytes, 0/48 segments ;;; Dynamic-1-Area 6K/3071K bytes, 1/48 segments ;;; Discardable-Static-Area 2K/64K bytes, 1/1 segment ;;; Discardable-Read-Write-Area 653K/704K bytes, 11/11 segments ;;; Foreign Area 37K/64K bytes, 1/1 segment ;;; Static-Area 523K/576K bytes, 9/9 segments ;;; Read-Write-Area 703K/704K bytes, 11/11 segments ;;; Readonly-Pointer-Area 1144K/1152K bytes, 18/18 segments ;;; Readonly-Non-Pointer-Area 3770K/3776K bytes, 59/59 segments > (gc-off) ;;; GC: 1372 words [5488 bytes] of dynamic storage in use. ;;; 785058 words [3140232 bytes] of free storage available ;;; before a GC. 1571488 words [6285952 bytes] of free ;;; storage available if GC is disabled. ;;; Disabling ephemeral garbage collection. T> (room t) ;;; 1444 words [5776 bytes] of dynamic storage in use. ;;; 1571418 words [6285672 bytes] of free storage available ;;; before a GC. 1571418 words [6285672 bytes] of free ;;; storage available if GC is disabled. ;;; Semi-space Size: 3072K bytes [48 segments] ;;; Current Dynamic Area: Dynamic-0-Area ;;; GC Status: Disabled ;;; Reserved Free Space: 6656K bytes [104 segments] ;;; Memory Growth Limit: 32768K bytes [512 segments], total ;;; Memory Growth Rate: 256K bytes [4 segments] ;;; Reclamation Ratio: 33% desired free after garbage collection ;;; Area Information: ;;; Name Size [used/allocated] ;;; ---- ---- ;;; Dynamic-0-Area 6K/4672K bytes, 1/73 segments ;;; Dynamic-1-Area 0K/3072K bytes, 0/48 segments ;;; Discardable-Static-Area 2K/64K bytes, 1/1 segment ;;; Discardable-Read-Write-Area 653K/704K bytes, 11/11 segments ;;; Foreign Area 37K/64K bytes, 1/1 segment ;;; Static-Area 523K/576K bytes, 9/9 segments ;;; Read-Write-Area 703K/704K bytes, 11/11 segments ;;; Readonly-Pointer-Area 1144K/1152K bytes, 18/18 segments ;;; Readonly-Non-Pointer-Area 3770K/3776K bytes, 59/59 segments NIL
egc-on
gc-on
> (gc-off) ;;; Disabling ephemeral garbage collection. T> (room t) ;;; 1650 words [6600 bytes] of dynamic storage in use. ;;; 1571212 words [6284848 bytes] of free storage available ;;; before a GC. 1571212 words [6284848 bytes] of free ;;; storage available if GC is disabled. ;;; Semi-space Size: 3072K bytes [48 segments] ;;; Current Dynamic Area: Dynamic-0-Area ;;; GC Status: Disabled ;;; Reserved Free Space: 6656K bytes [104 segments] ;;; Memory Growth Limit: 32768K bytes [512 segments], total ;;; Memory Growth Rate: 256K bytes [4 segments] ;;; Reclamation Ratio: 33% desired free after garbage collection ;;; Area Information: ;;; Name Size [used/allocated] ;;; ---- ---- ;;; Dynamic-0-Area 7K/4672K bytes, 1/73 segments ;;; Dynamic-1-Area 0K/3072K bytes, 0/48 segments ;;; Discardable-Static-Area 2K/64K bytes, 1/1 segment ;;; Discardable-Read-Write-Area 653K/704K bytes, 11/11 segments ;;; Foreign Area 37K/64K bytes, 1/1 segment ;;; Static-Area 523K/576K bytes, 9/9 segments ;;; Read-Write-Area 703K/704K bytes, 11/11 segments ;;; Readonly-Pointer-Area 1144K/1152K bytes, 18/18 segments ;;; Readonly-Non-Pointer-Area 3770K/3776K bytes, 59/59 segments NIL
> (gc-on) T
> (egc-on) ;;; Enabling ephemeral garbage collection. T
> (room t) ;;; 2324 words [9296 bytes] of dynamic storage in use. ;;; 783850 words [3135400 bytes] of free storage available ;;; before a GC. 1570024 words [6280096 bytes] of free ;;; storage available if GC is disabled. ;;; Semi-space Size: 3072K bytes [48 segments] ;;; Current Dynamic Area: Dynamic-0-Area ;;; GC Status: Enabled ;;; Reserved Free Space: 3712K bytes [58 segments] ;;; Memory Growth Limit: 32768K bytes [512 segments], total ;;; Memory Growth Rate: 256K bytes [4 segments] ;;; Reclamation Ratio: 33% desired free after garbage collection ;;; Area Information: ;;; Name Size [used/allocated] ;;; ---- ---- ;;; Ephemeral Level 0 1K/512K bytes, 1/8 segments ;;; Ephemeral Level 1 0K/640K bytes, 0/10 segments ;;; Ephemeral Level 2 0K/640K bytes, 0/10 segments ;;; Ephemeral Odd-level OSP 0K/512K bytes, 0/8 segments ;;; Ephemeral Even-level OSP 0K/640K bytes, 0/10 segments ;;; Dynamic-0-Area 10K/3071K bytes, 1/48 segments ;;; Dynamic-1-Area 0K/3072K bytes, 0/48 segments ;;; Discardable-Static-Area 2K/64K bytes, 1/1 segment ;;; Discardable-Read-Write-Area 653K/704K bytes, 11/11 segments ;;; Foreign Area 37K/64K bytes, 1/1 segment ;;; Static-Area 523K/576K bytes, 9/9 segments ;;; Read-Write-Area 703K/704K bytes, 11/11 segments ;;; Readonly-Pointer-Area 1144K/1152K bytes, 18/18 segments ;;; Readonly-Non-Pointer-Area 3770K/3776K bytes, 59/59 segments NIL
gc-off
*gc-silence*
*gc-silence*
controls dynamic garbage collection messages.
nil
nil
, garbage collection messages are displayed on the standard output. This value is the default.
t
t
, all garbage collection messages are suppressed.
:before
:before
.
:after
:after
.
:dynamic-expansion
:dynamic-expansion
.
:reserved-expansion
:reserved-expansion
.
*gc-silence*
procedures are called when normal memory allocation is impossible, an executing function that is bound to*gc-silence*
should not use more than the amount of storage that is reserved by the value of the keyword:reserved-dynamic
. This value defaults to 1024 bytes.
*gc-silence*
procedures are called when scheduling is inhibited, such procedures should not try to acquire process locks. It is an error for a process to acquire locks while scheduling is inhibited. You must write*gc-silence*
procedures carefully because many activities use locking; for example, writing to a window uses locks. For more information about process locks, see Chapter 5, "The Multitasking Facility" in The Advanced User's Guide.
> (room) ;;; 1968 words [7872 bytes] of dynamic storage in use. ;;; 522256 words [2089024 bytes] of free storage available ;;; before a GC. 1046480 words [4185920 bytes] of free ;;; storage available if GC is disabled. NILgc-size Function> (let ((*gc-silence* t)) (gc)) 5136 2091760 4188656
> (room) ;;; 1324 words [5296 bytes] of dynamic storage in use. ;;; 522900 words [2091600 bytes] of free storage available ;;; before a GC. 1047124 words [4188496 bytes] of free ;;; storage available if GC is disabled. NIL
gc-size
gc-size
returns the following multiple values:gc
.
> (gc-size) 5496 2091400 4188296;;; Write a simple macro that uses GC-SIZE to determine how ;;; much dynamic storage is used by a function. This macro ;;; assumes that ephemeral garbage collection is off. > (defmacro how-much-consing-macro (&body body) '(let ((initial-gc-size (gc-size))) (unwind-protect (progn ,@body) ; multiple-value-prog1 (format t "~&~S words consed" ; that doesn't cons. (floor (- (gc-size) initial-gc-size) 4))))) ; 4 bytes per word. HOW-MUCH-CONSING-MACRO
> (defun how-much-consing (fn &rest args) (how-much-consing-macro (apply fn args))) HOW-MUCH-CONSING
> (compile 'how-much-consing) HOW-MUCH-CONSING
> (defun test (x y) (cons x y)) TEST
> (how-much-consing 'test 'a 'b) 100 words consed (A . B)
> (compile 'test) TEST
> (how-much-consing 'test 'a 'b) 2 words consed (A . B)
gc
, room
room &key :default &optional verbosity
room
prints information about the current state of internal memory on the gc output stream. The gc output is defined by the value of the Common Lisp variable*gc-output*
, which is a synonym stream that writes to*trace-output*
.
nil
, a terse summary is printed. If it is non-nil
, a verbose description is given. If no argument is specified, or if the keyword argument:default
is specified,room
prints a moderate amount of information.
> (room nil) ;;; 775824 words [3103296 bytes] free NILwith-normal-consing-area Macro> (room) ;;; 792 words [3168 bytes] of dynamic storage in use. ;;; 523238 words [2092952 bytes] of free storage available before a GC. ;;; 1047268 words [4189072 bytes] of free storage available if GC is disabled. T
> (room :default) ;;; 792 words [3168 bytes] of dynamic storage in use. ;;; 523238 words [2092952 bytes] of free storage available before a GC. ;;; 1047268 words [4189072 bytes] of free storage available if GC is disabled. T
> (room t) ;;; 10350 words [41400 bytes] of dynamic storage in use. ;;; 775824 words [3103296 bytes] of free storage available ;;; before a GC. 1561998 words [6247992 bytes] of free ;;; storage available if GC is disabled. ;;; Semi-space Size: 3072K bytes [48 segments] ;;; Current Dynamic Area: Dynamic-1-Area ;;; GC Status: Enabled ;;; Reserved Free Space: 3200K bytes [50 segments] ;;; Memory Growth Limit: 32768K bytes [512 segments], total ;;; Memory Growth Rate: 256K bytes [4 segments] ;;; Reclamation Ratio: 33% desired free after garbage collection
;;; Area Information: ;;; Name Size [used/allocated] ;;; ---- ---- ;;; Ephemeral Level 0 1K/512K bytes, 1/8 segments ;;; Ephemeral Level 1 0K/640K bytes, 0/10 segments ;;; Ephemeral Level 2 0K/640K bytes, 0/10 segments ;;; Ephemeral Odd-level OSP 0K/512K bytes, 0/8 segments ;;; Ephemeral Even-level OSP 0K/640K bytes, 0/10 segments ;;; Dynamic-0-Area 0K/3072K bytes, 0/48 segments ;;; Dynamic-1-Area 41K/3071K bytes, 1/48 segments ;;; Discardable-Static-Area 513K/576K bytes, 9/9 segments ;;; Foreign Area 37K/64K bytes, 1/1 segment ;;; Static-Area 523K/576K bytes, 9/9 segments ;;; Read-Write-Area 703K/704K bytes, 11/11 segments ;;; Readonly-Pointer-Area 1144K/1152K bytes, 18/18 segments ;;; Readonly-Non-Pointer-Area 3771K/3776K bytes, 59/59 segments NIL
with-normal-consing-area
{form}*
with-normal-consing-area
causes objects to be created in ephemeral space when ephemeral garbage collection is turned on and in dynamic space when ephemeral garbage collection is off.
with-static-area
with-static-area
{form}*
with-static-area
evaluates a series of forms while forcing all consing to be done in the static area. Objects that are created are permanent and are never reclaimed by garbage collection.
with-static-area
should be used with short series of forms to prevent the unintentional creation of objects in static space.
;;; This example assumes you have a large defstruct called ;;; large-object that creates large-object structures ;;; that are retained indefinitely. Creating ;;; large-object structures in the static area will ;;; therefore speed garbage collection. The following code ;;; provides a wrapper to the defstruct constructor for ;;; large-object so that large-object structures are ;;; created in the static area: > (defstruct (large-object (:constructor new-large-object)) field-1 field-2 field-3 field-4 field-n) LARGE-OBJECT> (compile (defun make-large-object (&rest arguments) (with-static-area (apply #'new-large-object arguments)))) MAKE-LARGE-OBJECT
> (make-large-object :field-4 35) #S(LARGE-OBJECT FIELD-1 NIL FIELD-2 NIL FIELD-3 NIL FIELD-4 35 FIELD-N NIL)
;; Be cautious when using with-static-area. For example, the ;; following function could unintentionally cons in the static ;; area. If X is a bignum, the product and any intermediate ;; consing during the multiplication will occupy static space. ;; Furthermore, if the multiplication causes an error, the ;; error handler will cons in static space. > (compile (defun cons-in-static (x) (with-static-area (new-large-object :field-1 (* x x))))) CONS-IN-STATIC ;; The following function avoids extra consing in the static ;; space. > (compile (defun create-in-static (x) (let ((f1 (* x x))) (with-static-area (new-large-object :field-1 f1))))) CREATE-IN-STATIC
;; The following function shows another way to avoid extra ;; consing in static space. > (compile (defun create-in-static2 (x) (with-static-area (new-large-object :field-1 (with-normal-consing-area (* x x)))))) CREATE-IN-STATIC2
with-normal-consing-area
Generated with Harlequin WebMaker